I mean to return a variable Drawable
that stores an image but load it by its location in the folder /drawable
where it is within the same project.
Something like that:
Drawable imagen = getDrawable("imagen.png"); //Es ejemplo, esto no existe.
and then add it to a ImageView
.
Using this scheme:
With that method you can get the Drawable, replace the +++ with the name of your drawable
Using a URI
There are several ways to get the image stored inside
/drawable
.The most common way (Use
ContextCompat
, to avoid the deprecated methodgetDrawable()
)you can also use the URI to get the image path inside
/drawable
:But the easiest option, just use setImageResource , defining the id of the resource.
With all 3 options you get the same result.
I imagine you want to get the image that the user selected. I attach two methods, the first is the one that triggers the opening gallery, and the second is the one as the answer once I select it.
That might give you an idea. Note that this code is inside a Fragment, if you have it in an Activity it would change a bit, you would remove getActivity(). and what is not necessary to make implicit in an Activity.
Cheers