-1

I have String field with Drawable image, this image is in @Drawable folder I have only a String path!

I want to ask how I can retrieve my image from a path, because getDrawable is deprecated

Uri uri=Uri.parse(drawableImage);
int imageResource = getResources().getIdentifier(drawableImage, null, getActivity().getPackageName());
weatherIcon.setImageDrawable(getResources().getDrawable(imageResource));

how I can do? getDrawable is deprecated

1
  • Add more description on what you want to get?
    – Xenolion
    Oct 19, 2017 at 12:47

1 Answer 1

2

The method getDrawable() is deprecated and the alternative is

ContextCompat.getDrawable(context, R.drawable.***);

But if you have a String name of the Image. Then here is what you have to do:

getResources().getIdentifier("image_name_in_drawable_folder", "drawable", getActivity().getPackageName());

Do not put the full path from the Uri. Just put the identifier name only the image name only.

20
  • ok ok, i understand but i dont have R.drawable.*** i have String url = "R.drawable.***"; how i can do? Oct 19, 2017 at 13:39
  • I dont get you but Is that drawable present in your project files Drawable folder???
    – Xenolion
    Oct 19, 2017 at 13:41
  • i try... i used: String drawableImage = "q"+urlImage; int imageResource = getResources().getIdentifier(drawableImage, null, getActivity().getPackageName()); weatherIcon.setImageDrawable( (ContextCompat.getDrawable(getContext(), imageResource))); Oct 19, 2017 at 13:53
  • If your drawable is in Drawable folder then you should use R.drawable.your_drawable_name. I dont get it when you say you have a string path while the drawable is in your apk??
    – Xenolion
    Oct 19, 2017 at 13:55
  • i takeDown image's name from JSON file... is a String, this String is: 01, so i need to add some letter before and put .pgn to have the resource image to retrieve in my Drawable folder... yes i store all the possibility image in my Drawable folder! and i dont know how to do! Oct 19, 2017 at 14:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.