如何在android中动态设置drawable中的图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11737607/
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
How to set the image from drawable dynamically in android?
提问by hemanth kumar
I am storing my project related images in drawable folder. Also I am storing the image names in string variable and dynamically I am trying to set those images to the imageview. But the image is not displaying. Please help me in this regard.
我将与项目相关的图像存储在 drawable 文件夹中。此外,我将图像名称存储在字符串变量中,并动态地尝试将这些图像设置为图像视图。但图像不显示。请在这方面帮助我。
My Code:
我的代码:
int res = getResources().getIdentifier(imagename, "drawable", this.getPackageName());
imageview= (ImageView)findViewById(R.id.imageView);
imageview.setImageResource(res);
In the above code "imagename" is the string variable which contains the image name.
在上面的代码中,“imagename”是包含图像名称的字符串变量。
Thanks in advance
提前致谢
回答by rfsbraz
Try this:
尝试这个:
String uri = "@drawable/myresource"; // where myresource (without the extension) is the file
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
imageview= (ImageView)findViewById(R.id.imageView);
Drawable res = getResources().getDrawable(imageResource);
imageView.setImageDrawable(res);
回答by Ram kiran
Here i am setting the frnd_inactive
image from drawable
to the image
在这里,我将frnd_inactive
图像设置drawable
为图像
imageview= (ImageView)findViewById(R.id.imageView);
imageview.setImageDrawable(getResources().getDrawable(R.drawable.frnd_inactive));
回答by Georgy Gobozov
imageview= (ImageView)findViewById(R.id.imageView);
imageview.setImageResource(R.drawable.mydrawable);
回答by Chintan Raghwani
Try this Dynamic code
试试这个动态代码
String fnm = "cat"; // this is image file name
String PACKAGE_NAME = getApplicationContext().getPackageName();
int imgId = getResources().getIdentifier(PACKAGE_NAME+":drawable/"+fnm , null, null);
System.out.println("IMG ID :: "+imgId);
System.out.println("PACKAGE_NAME :: "+PACKAGE_NAME);
// Bitmap bitmap = BitmapFactory.decodeResource(getResources(),imgId);
your_image_view.setImageBitmap(BitmapFactory.decodeResource(getResources(),imgId));
In above code you will need Image-file-Name and Image-View object which both you are having.
在上面的代码,你需要图片文件名称和图像的视图对象这两者你有。
回答by Nirali
See below code this is working for me
请参阅下面的代码,这对我有用
iv.setImageResource(getResources().getIdentifier(
"imagename", "drawable", "com.package.application"));
回答by cmcoffee91
This works for me (dont use the extension of the image, just the name):
这对我有用(不要使用图像的扩展名,只使用名称):
String imagename = "myImage";
int res = getResources().getIdentifier(imagename, "drawable", this.getPackageName());
imageview= (ImageView)findViewById(R.id.imageView);
imageview.setImageResource(res);
回答by Faustino Gagneten
Here is the best way that works in every phone at today. Most of those answer are deprecated or need an API >= 19 or 22. You can use the fragment code or the activity code depends what you need.
这是当今适用于每部手机的最佳方式。大多数答案已弃用或需要 API >= 19 或 22。您可以使用片段代码或活动代码,具体取决于您的需要。
Fragment
分段
//setting the bitmap from the drawable folder
Bitmap bitmap= BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.my_image);
//set the image to the imageView
imageView.setImageBitmap(bitmap);
Activity
活动
//setting the bitmap from the drawable folder
Bitmap bitmap= BitmapFactory.decodeResource(MyActivity.this.getResources(), R.drawable.my_image);
//set the image to the imageView
imageView.setImageBitmap(bitmap);
Cheers!
干杯!
回答by emilpmp
getDrawable()
is deprecated, so try this
getDrawable()
已弃用,所以试试这个
imageView.setImageResource(R.drawable.fileName)
回答by Karan Datwani
imageView.setImageResource(R.drawable.picname);
回答by Venky
First of let's your image name is myimage. So what you have to do is that go to Drawable and save the image name myimage.
首先让你的图像名称是myimage。所以你要做的就是转到 Drawable 并保存图像名称myimage。
Now assume you know only image name and you need to access it. Use below snippet to access it,
现在假设您只知道图像名称并且需要访问它。使用以下代码段访问它,
what you did is correct , ensure you saved image name you are going to use inside coding.
你所做的是正确的,确保你保存了你将在编码中使用的图像名称。
public static int getResourceId(Context context, String name, String resourceType) {
return context.getResources().getIdentifier(toResourceString(name), resourceType, context.getPackageName());
}
private static String toResourceString(String name) {
return name.replace("(", "")
.replace(")", "")
.replace(" ", "_")
.replace("-", "_")
.replace("'", "")
.replace("&", "")
.toLowerCase();
}
In addition to it you should ensure that there is no empty spaces and case sensitives
除此之外,您应该确保没有空格和区分大小写