Android - Open resource from @drawable String

Android

Android Problem Overview


I have:

String uri = "@drawable/myresource.png";

How can I load that in ImageView? this.setImageDrawable?

Android Solutions


Solution 1 - Android

If you really need to work with a string, try something like this:

private void showImage() {
	String uri = "drawable/icon";
	
	// int imageResource = R.drawable.icon;
	int imageResource = getResources().getIdentifier(uri, null, getPackageName());
	
	ImageView imageView = (ImageView) findViewById(R.id.myImageView);
	Drawable image = getResources().getDrawable(imageResource);
	imageView.setImageDrawable(image);
}

Else I would recommend you to work with R.* references like this:

int imageResource = R.drawable.icon;
Drawable image = getResources().getDrawable(imageResource);

Solution 2 - Android

First, don't do that, as that @drawable syntax is meaningless in Java code. Use int resourceId=R.drawable.myresource.

If for some reason you do wind up a resource name and need the integer ID, use getIdentifier() on the Resources object.

Solution 3 - Android

you can also add your own variable.. in my case scene.name between i followed @pfleidi answers. c stands for context.

String uri = "drawable/" + scene.name; 
int imageResource = c.getResources().getIdentifier(uri, null, c.getPackageName());
    		
imageList.setImageResource(imageResource);

Solution 4 - Android

Long since overdue, but my favorite is to use the Context:

context.getApplicationContext().getResources().getDrawable(R.drawable.placeholder_image)

where placeholder_image is the id of the resource. In your case, R.drawable.myresource.

Context can be an activity or the application. Pretty much wherever you are has reference to the context, which you can use to get the application's context.

Solution 5 - Android

In case anyone else comes across the problem I had, I was loading the image name from JSON and then needed to get the identifier. The second parameter of getIdentifier can be set to "drawable". This is a slight variation on @alia-ramli-ramli answer...

String uri = details.getString("circle").toLowerCase();

int imageResource = context.getResources().getIdentifier(uri, "drawable", context.getPackageName());
Drawable image = context.getResources().getDrawable(imageResource);
viewHolder.unit_circle.setImageDrawable(image);

Solution 6 - Android

You can check [here][1]

int resID = mContext.getResources().getIdentifier(stringfilename, "drawable", mContext.getPackageName());
iv_imageview.setImageDrawable(ContextCompat.getDrawable(mContext,resID)); 

you can also retrieve image from mipmap by replacing "drawable" with "mipmap" parameter of getIdentifier() method. [1]: https://developer.android.com/reference/android/content/res/Resources#getIdentifier%28java.lang.String,%20java.lang.String,%20java.lang.String%29

Solution 7 - Android

I had the same problem with ... the deprecated etc. etc. I solved in such a way with minimum API 14 okay.

...
...
ImageView imgPoster = viewCache.getImgPoster(resource);
String uri = "drawable/" + film.getPoster();
int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
Drawable image = ContextCompat.getDrawable(context, imageResource);
imgPoster.setImageDrawable(image);

return convertView;
...
...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionnikib3roView Question on Stackoverflow
Solution 1 - AndroidpfleidiView Answer on Stackoverflow
Solution 2 - AndroidCommonsWareView Answer on Stackoverflow
Solution 3 - AndroidAlia Ramli RamliView Answer on Stackoverflow
Solution 4 - AndroidWendelView Answer on Stackoverflow
Solution 5 - AndroidFlexicoderView Answer on Stackoverflow
Solution 6 - AndroidSanjay BhalaniView Answer on Stackoverflow
Solution 7 - AndroidMaurizioView Answer on Stackoverflow