-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

Dhaval Solanki
  • 4,093
  • 1
  • 21
  • 33

1 Answers1

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.

Xenolion
  • 9,344
  • 6
  • 25
  • 39
  • ok ok, i understand but i dont have R.drawable.*** i have String url = "R.drawable.***"; how i can do? – francesco freddi Oct 19 '17 at 13:39
  • I dont get you but Is that drawable present in your project files Drawable folder??? – Xenolion Oct 19 '17 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))); – francesco freddi Oct 19 '17 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 '17 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! – francesco freddi Oct 19 '17 at 14:39
  • Dont save image names in json as tring save integers for example R.drawable.your_image is an `int` save that integer instead of a name as a string its hard to convert back because android consider it as an int. So take each R.drawable.image save that as an integer and not a string. Dont put double qoutes. – Xenolion Oct 19 '17 at 14:46
  • i really dont understand... i have image, that i saved in Drawable folder... after i retrieve name to JSON, i built right name (put letter before and .png) as a String (because i have only String)...... i need to retrieve this image THAT I ALREADY HAVE!!!!!!!!!! i cannot understand (or i dont know i suppose) how i can save int value for the images! how i can save integer instead of a name as a String.... this i dont understand!! thankYouSoMuchForThisHelpl – francesco freddi Oct 20 '17 at 07:21
  • Okay lets go slowly where does you take the json from?. Is it from internet? – Xenolion Oct 20 '17 at 07:24
  • I have updated my answer please see if it works @francescofreddi – Xenolion Oct 20 '17 at 08:24
  • i have String drawableImage = urlImage+"q"+iconaMeteo+".png"; that is: R.Drawable.q10d.png.... this image is in Drawable folder... when i do: int imageResource = getResources().getIdentifier(drawableImage, null, getActivity().getPackageName()); to retrieve int resource (like your suggest) crash!! :( – francesco freddi Oct 20 '17 at 10:19
  • So take each R.drawable.image save that as an integer and not a string.............. i cannot understand! really.... – francesco freddi Oct 20 '17 at 10:21
  • to sum up: i need to retrieve image in my Drawable folder from String name: R.Drawable.q10d.png i dont know how i can do! (i am in a Fragment class, if is need it) – francesco freddi Oct 20 '17 at 10:36
  • Using a string I have updated and edit my answer. Check above in place of `image_name_in_drawable_folder` put there only the name `q10d` and if it doesnt work put `q10d.png` but I think it will work with just `q10d`. Check the answer above I have edited it. – Xenolion Oct 20 '17 at 10:41
  • i try... is always 0!!! int imageResource = getResources().getIdentifier("q01d", null, getActivity().getPackageName()); or i try... is always 0!!! int imageResource = getResources().getIdentifier("q01d.png", null, getActivity().getPackageName()); :( – francesco freddi Oct 20 '17 at 12:11
  • function functio: int imageResource = getResources().getIdentifier(drawableImage, "drawable", getActivity().getPackageName()); with this all functionnnnn yuhhuuuuu thankYouSoMuchForHelp, i love you!!! francesco – francesco freddi Oct 20 '17 at 12:22
  • Okay @francescofreddi but because my solution has solved your problem then you have to Accept it as an answer by putting a tick to it. And you are the only one who can do that. This was your question. – Xenolion Oct 20 '17 at 13:20
  • votes cast by those with less than 15 reputation are recorded, but do no change the the publicly displayed post score...... i am :( – francesco freddi Oct 20 '17 at 13:43
  • Dont vote just put a tick! You can vote for now! – Xenolion Oct 20 '17 at 14:10
  • ok, i do, i think :) – francesco freddi Oct 20 '17 at 14:50
  • Okay. Good and thanks! – Xenolion Oct 20 '17 at 14:54