I have followed several manuals and seen several answers on the subject, but my limited knowledge allows me to find the solution, that's why I turn to you.
I want to upload a member's image to a activity
. I have the images in the folder drawables
.
If I load the image individually using R.dragable.3904123
, the image loads without problems
If I use variables to get that same address of the image, the image is not displayed.
private void metodoCargarImagen() {
//este método lo usaba para obtener la URL de la imagen del servidor, ahora obtengo(R.dragable.)
String imagenURL = miConexion.metodoEnviarRutaImagenes();
//este método lo utilizo para obtener el nombre de la imagen, en este caso(3904123)
String nImagen = metodoObtenerNumeroImagen(socio);
//aqui concateno (R.dragable. + a + 3904123)
String rutaImg = imagenURL + "a" + nImagen;
//convierto el String en URI ?
Uri uriImagen = Uri.parse(rutaImg);
//Agrega imagen al ImageView.
Picasso.with(this)
.load(uriImagen) //con este método, a la derecha me aparece el texto "RequestCreator"
.error(R.drawable.avestruz)
.fit()
.centerInside()
.into(img_vS_img);
}
This is the image code:
<ImageView
android:id="@+id/img_vS_img"
android:layout_width="102dp"
android:layout_height="145dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/avestruz" />
This would be the code loading the same image address, but individually:
private void metodoCargarImagen() {
String imagenURL = miConexion.metodoEnviarRutaImagenes();
String nImagen = metodoObtenerNumeroImagen(socio);
//Uri rutaImg = imagenURL + "a" + nImagen + extImg;
String rutaImg = imagenURL + "a" + nImagen;
//Uri uriImage = Uri.parse("android.resource://" + getPackageName() +"/"+ rutaImg);
//Crea ruta de la imagen.
//rutaImg = rutaImg.replace("@drawable/", "android.resource://"+ getPackageName() +"/drawable/");
//Obtiene la uri de la imagen.
Uri uriImagen = Uri.parse(rutaImg);
//Agrega imagen al ImageView.
Picasso.with(this)
.load(R.drawable.a3904123) //Me sigue apareciendo "RequestCreator" pero la imagen es visible
.error(R.drawable.avestruz)
.fit()
.centerInside()
.into(img_vS_img);
}
The possibility of loading the image without converting it into Uri, I had previously discarded it because it didn't work for me either
If you're using
Picasso
you don't have to cast the url toURI
, you can load the url to load the image, just make sure the variablerutaImg
contains a valid image url.R.drawable.a3904123
is aint
that corresponds to the id of the image. It is not the same as string"R.drawable.a3904123"
. Converting it to uri doesn't make it valid either. In fact that step doesn't make any difference because it's what Picasso does internally when you pass it a string.For the uri to be valid it must have this format
But you can also load the image from its id