Android - Open resource from @drawable String
Android - Open resource from @drawable String
I have:
String uri = "@drawable/myresource.png";
How can I load that in ImageView? this.setImageDrawable?
Answer by CommonsWare for Android - Open resource from @drawable String
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.
Answer by pfleidi for Android - Open resource from @drawable String
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);
Answer by Wendel for Android - Open resource from @drawable String
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.
Answer by Alia Ramli Ramli for Android - Open resource from @drawable String
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);
Answer by Flexicoder for Android - Open resource from @drawable String
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);
Answer by Maurizio for Android - Open resource from @drawable String
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; ... ...
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment