Android - 从 @drawable 字符串打开资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2349652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - Open resource from @drawable String
提问by nikib3ro
I have:
我有:
String uri = "@drawable/myresource.png";
How can I load that in ImageView? this.setImageDrawable?
如何在 ImageView 中加载它?this.setImageDrawable?
回答by pfleidi
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:
否则,我建议您使用 R.* 引用,如下所示:
int imageResource = R.drawable.icon;
Drawable image = getResources().getDrawable(imageResource);
回答by CommonsWare
First, don't do that, as that @drawablesyntax is meaningless in Java code. Use int resourceId=R.drawable.myresource.
首先,不要这样做,因为该@drawable语法在 Java 代码中毫无意义。使用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 Resourcesobject.
如果由于某种原因你确实结束了资源名称并需要整数 ID,请getIdentifier()在Resources对象上使用。
回答by Alia Ramli Ramli
you can also add your own variable.. in my case scene.name between i followed @pfleidi answers. c stands for context.
你也可以添加你自己的变量..在我的情况下,我遵循@pfleidi答案之间的scene.name。c 代表上下文。
String uri = "drawable/" + scene.name;
int imageResource = c.getResources().getIdentifier(uri, null, c.getPackageName());
imageList.setImageResource(imageResource);
回答by Wendel
Long since overdue, but my favorite is to use the Context:
早就应该了,但我最喜欢的是使用上下文:
context.getApplicationContext().getResources().getDrawable(R.drawable.placeholder_image)
where placeholder_imageis the id of the resource. In your case, R.drawable.myresource.
其中placeholder_image是资源的 ID。在你的情况下,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.
上下文可以是活动或应用程序。无论您身在何处,几乎都可以引用上下文,您可以使用它来获取应用程序的上下文。
回答by Flexicoder
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...
万一其他人遇到我遇到的问题,我正在从 JSON 加载图像名称,然后需要获取标识符。getIdentifier 的第二个参数可以设置为“drawable”。这是@alia-ramli-ramli 答案的细微变化...
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);
回答by Sanjay Bhalani
You can check here
你可以在这里查看
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.
您还可以通过将“drawable”替换为 getIdentifier() 方法的“mipmap”参数来从 mipmap 中检索图像。
回答by Maurizio
I had the same problem with ... the deprecated etc. etc. I solved in such a way with minimum API 14 okay.
我有同样的问题......弃用等。我以这种方式解决了最低 API 14 好的问题。
...
...
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;
...
...

