I am trying to find a way to set an imageview via a string. It seems that getResources() is deprecated and I can't seem to find a fast way to pass a string and load an image to my imageView. I do not have any working or useful code to present, I am simply asking for methods to look into. Thanks!
-
1I'm sorry, this question makes no sense to me at all. Can you point me to the doc for exactly which getResources is deprecated? How exactly do you intend to convert a string into an image? – Doug Stevenson Feb 13 '16 at 08:09
-
Here is an example: http://stackoverflow.com/questions/5254100/how-to-set-an-imageviews-image-from-a-string I have the names of the images I wish to access, so I wish to access them from drawable by their names. – TheYargonaut Feb 13 '16 at 08:10
-
I can't see anything in the Docs about `getResources()` being deprecated: http://developer.android.com/reference/android/content/Context.html#getResources() – PPartisan Feb 13 '16 at 08:18
-
This is what I got it from http://stackoverflow.com/questions/29041027/android-getresources-getdrawable-deprecated-api-22 – TheYargonaut Feb 13 '16 at 08:28
-
`getDrawable(int id)` is deprecated as of API 22 (it has been superseded by `getDrawable(int id, Theme theme)`), but `getResources()` is not deprecated. – PPartisan Feb 13 '16 at 08:43
3 Answers
with API 22 'getResources()' is deprecated. You can use this instead, it also backward compatible so you can safely move on to this method.
ContextCompat.getDrawable(context, R.drawable.<you_name_it>)
Update:
Also forgot to mention that you can use Picasso to load your strings (aiming to picture's path) into imageviews.
here a little example:
ImageView imageView1 = (ImageView) findViewById(R.id.myImageView);
Picasso.with(getApplicationContext()).load("www.pictures.com/picture.jpg").into(imageView1);
you can make a little search about Picasso here: http://square.github.io/picasso/
not to mention but Stackoverflow Android app also uses this library, one of the must-have libraries in Android.
-
Thank you, but how would I access my images dynamically? I want to pass a string, not static – TheYargonaut Feb 13 '16 at 08:16
-
1
-
-
-
The answer by theMatus is correct. Drawables are accessed via an int
identifier (i.e. R.drawable.some_image
will have a corresponding int
value). To convert a String
to such a value, you would use Resources.getIdentifier()
:
String mDrawableName = "some_image";
int resId = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
To access the Drawable
, you would use getResources().getDrawable(resId)
. getResources()
is not deprecated by the way, although getDrawable(int id)
technically is as of API 22 (it will still work). If you want to approach this correctly and get around the deprecation, you can use the following:
@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
private Drawable getDrawableForSdkVersion(int resId, Resources res) {
Drawable drawable = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
drawable = res.getDrawable(resId, null);
} else {
drawable = res.getDrawable(resId);
}
return drawable;
}

- 8,173
- 4
- 29
- 48
ImageView imgView = (ImageView) findViewById(R.id.imageView);
imgView.setImageDrawable(ContextCompat.getDrawable(currentActivity.this,R.drawable.your_image));
Actually theMatus answered your question. This code is for deprecated problem.

- 107
- 1
- 6