Image in Drawable Get route URI Android


I would like to be able to put an "x" image by hand in the Drawable folder of android and be able to obtain the Uri route of that image, so that I can then do

Img.setImageURI(aca quisiera la Uri de esa imagen que agrege a la carpeta Drwable)

because this is because I'm getting all the contacts and its image I have the image in uri format and if it does not have an image that contact I want to put one that I added to my project, as my path from which it grabs the data until that I show them is going through many activities I would like to be able to get the URI of an x image that is in the Drawable folder, thanks in advance

asked by Bruno Sosa Fast Tag 26.10.2017 в 15:03

2 answers


Upload an image in /drawable from your Uri.

If you want to use the method setImageURI () to load the image inside your ImageView , it is necessary to specify the correct path to the resource, since otherwise it will not load, the path is "android.resource:// and the packagename of your project.

The correct way to do it is:

  Uri uriImage = Uri.parse("android.resource://" + getPackageName() +"/"+R.drawable.imagen);

On the other hand, if you only specify% as resource% of the resource:

Uri uriImagen = Uri.parse("R.drawable.imagen");

this will not work.

Even from your Uri you can get a Uri and load it in your Drawable using the ImageView method:

String uri = "@drawable/imagen";
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
Drawable imagen = ContextCompat.getDrawable(getApplicationContext(), imageResource);
answered by 26.10.2017 / 19:44

If what you want is the URL of the resource you can use the class Uri sending it as parameter the identifier of drawable like this:

Uri uriImagen = Uri.parse("R.drawable.mi_imagen");

You can also use the setImageResource(int resourceId) method of ImageView where you accept the id of the resource to load the image:

answered by 26.10.2017 в 15:15