How to show a local folder image to an "ImageView"

2

I'm trying to show an image that is stored in the "drawable" folder and I'm using'setImageURI 'but for some reason I'm not sure the code is not working for me.

The information that I am uploading in the app is from a json file in which I have the name, location and address of the image.

to get the address of the image I'm using the method, which gives me a string with the address of the image.

  

student.getImage ();

Thanks to Julian I know that I can use: NuevaView.imagen.setImageResource (R.drawable.ci); to load the image, but it would always be the same image for all. There is some way to load the address of each image.

Try using setImageURI but I do not see anything in the palicacion, I leave blank the section of the image .. the json file has 20 members each with its name, location and a single image assigned to each one.

Something idea how I can solve this problem.

String img = estudiante.getImagen();
File imagenArchivo= new  File(img);
NuevaView.imagen.setImageURI(Uri.fromFile(imagenArchivo));

Here is the ImageView that I am using

<ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".1"
        app:srcCompat="@drawable/cc"/>

and here the code to show the image

 if (vista== null) {
   vista= inflater.inflate(R.layout.lista, viewGroup, false);
   NuevaView= new NewView();
   NuevaView.nombre= (TextView) vista.findViewById(R.id.Nombre);
   NuevaView.locacion = (TextView) vista.findViewById(R.id.Locacion);
   NuevaView.imagen = (ImageView) vista.findViewById(R.id.imageView);
            vista.setTag(NewView);
        } else
   NewView = (NewView) view.getTag();

        NuevaView.name.setText(estudiante.getNombre());
        NuevaView.location.setText(estudiante.getLocacion()); 
        String img = estudiante.getImagen();
        File imagenArchivo= new  File(img);
        NuevaView.imagen.setImageURI(Uri.fromFile(imagenArchivo));

            return vista;
    }
        public class NuevaView{
            TextView nombre;
            TextView locacion;
            ImageView imagen;
        }
    
asked by Pedro 29.06.2018 в 18:09
source

1 answer

1

The string you receive as an image of json is:

 "@drawable/ci"

that refers to an image of name ci that is inside the folder /drawable , it is very important that this image or the images indicated by the feed as an image within "@drawable/" must exist in the project.

To load the image, it must be done in this way, first remember that to access a project resource in the /drawable directory, it can be done through this route:

"android.resource://<PAQUETE DE APLICACION>/drawable/"

Therefore this would be the appropriate way to load the image:

String img = estudiante.getImagen();
//Crea ruta de la imagen.
img = img.replace("@drawable/", "android.resource://"+ getPackageName() +"/drawable/");
//Obtiene la uri de la imagen.
Uri uriImagen = Uri.parse(img);
//Agrega imagen al ImageView.
NuevaView.imagen.setImageURI(uriImagen);
    
answered by 29.06.2018 / 22:54
source