如何在android中动态地设置图像?

时间:2021-11-16 00:32:31

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.

我正在将我的项目相关图像存储在可绘制文件夹中。我还将图像名称存储在string变量中,并动态地将这些图像设置为imageview。但是图像没有显示。在这方面请帮助我。

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

谢谢提前

14 个解决方案

#1


93  

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);

#2


33  

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));

#3


22  

imageview= (ImageView)findViewById(R.id.imageView);
imageview.setImageResource(R.drawable.mydrawable) 

#4


18  

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.

在上面的代码中,您将需要Image-file-Name和Image-View对象。

#5


7  

See below code this is working for me

请看下面的代码,这对我来说是有用的

iv.setImageResource(getResources().getIdentifier(
                "imagename", "drawable", "com.package.application"));

#6


4  

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);

#7


4  

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!

干杯!

#8


3  

imageView.setImageResource(R.drawable.picname);

#9


2  

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

除此之外,您应该确保没有空的空间和病例敏感器

#10


2  

As of API 22, getResources().getDrawable() is deprecated (see also Android getResources().getDrawable() deprecated API 22). Here is a new way to set the image resource dynamically:

在API 22中,getResources(). getdrawable()已被弃用(参见Android getResources(). getdrawable()已弃用API 22)。下面是一种动态设置图像资源的新方法:

String resourceId = "@drawable/myResourceName"; // where myResourceName is the name of your resource file, minus the file extension
int imageResource = getResources().getIdentifier(resourceId, null, getPackageName());
Drawable drawable = ContextCompat.getDrawable(this, imageResource); // For API 21+, gets a drawable styled for theme of passed Context
imageview = (ImageView) findViewById(R.id.imageView);
imageview.setImageDrawable(drawable);

#11


2  

getDrawable() is deprecated, so try this

不赞成使用getDrawable(),请尝试使用这个

imageView.setImageResource(R.drawable.fileName)

#12


0  

This works fine for me.

这对我来说没问题。

final R.drawable drawableResources = new R.drawable(); 
final Class<R.drawable> c = R.drawable.class;
final Field[] fields = c.getDeclaredFields();
for (int i=0; i < fields.length;i++) 
{
     resourceId[i] = fields[i].getInt(drawableResources);  
    /* till here you get all the images into the int array resourceId.*/
}
imageview= (ImageView)findViewById(R.id.imageView);
if(your condition)
{
   imageview.setImageResource(resourceId[any]);
}

#13


0  

getDrawable() is deprecated. now you can use

getDrawable()弃用。现在你可以使用

imageView.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.msg_status_client_read)) 

#14


0  

int[] phptr=new int[5];

adding image pointer to array

向数组添加图像指针

for(int lv=0;lv<3;lv++)
    {
        String id="a"+String.valueOf(lv+1);
        phptr[lv]= getResources().getIdentifier(id, "drawable", getPackageName());
    }

for(int loopvariable=0;loopvariable<3;loopvariable++) 
    {
        et[loopvariable] = findViewById(p[loopvariable]);
    }



for(int lv=0;lv<3;lv++)
    {
        et[k].setImageResource(phptr[k]);
    }

#1


93  

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);

#2


33  

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));

#3


22  

imageview= (ImageView)findViewById(R.id.imageView);
imageview.setImageResource(R.drawable.mydrawable) 

#4


18  

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.

在上面的代码中,您将需要Image-file-Name和Image-View对象。

#5


7  

See below code this is working for me

请看下面的代码,这对我来说是有用的

iv.setImageResource(getResources().getIdentifier(
                "imagename", "drawable", "com.package.application"));

#6


4  

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);

#7


4  

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!

干杯!

#8


3  

imageView.setImageResource(R.drawable.picname);

#9


2  

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

除此之外,您应该确保没有空的空间和病例敏感器

#10


2  

As of API 22, getResources().getDrawable() is deprecated (see also Android getResources().getDrawable() deprecated API 22). Here is a new way to set the image resource dynamically:

在API 22中,getResources(). getdrawable()已被弃用(参见Android getResources(). getdrawable()已弃用API 22)。下面是一种动态设置图像资源的新方法:

String resourceId = "@drawable/myResourceName"; // where myResourceName is the name of your resource file, minus the file extension
int imageResource = getResources().getIdentifier(resourceId, null, getPackageName());
Drawable drawable = ContextCompat.getDrawable(this, imageResource); // For API 21+, gets a drawable styled for theme of passed Context
imageview = (ImageView) findViewById(R.id.imageView);
imageview.setImageDrawable(drawable);

#11


2  

getDrawable() is deprecated, so try this

不赞成使用getDrawable(),请尝试使用这个

imageView.setImageResource(R.drawable.fileName)

#12


0  

This works fine for me.

这对我来说没问题。

final R.drawable drawableResources = new R.drawable(); 
final Class<R.drawable> c = R.drawable.class;
final Field[] fields = c.getDeclaredFields();
for (int i=0; i < fields.length;i++) 
{
     resourceId[i] = fields[i].getInt(drawableResources);  
    /* till here you get all the images into the int array resourceId.*/
}
imageview= (ImageView)findViewById(R.id.imageView);
if(your condition)
{
   imageview.setImageResource(resourceId[any]);
}

#13


0  

getDrawable() is deprecated. now you can use

getDrawable()弃用。现在你可以使用

imageView.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.msg_status_client_read)) 

#14


0  

int[] phptr=new int[5];

adding image pointer to array

向数组添加图像指针

for(int lv=0;lv<3;lv++)
    {
        String id="a"+String.valueOf(lv+1);
        phptr[lv]= getResources().getIdentifier(id, "drawable", getPackageName());
    }

for(int loopvariable=0;loopvariable<3;loopvariable++) 
    {
        et[loopvariable] = findViewById(p[loopvariable]);
    }



for(int lv=0;lv<3;lv++)
    {
        et[k].setImageResource(phptr[k]);
    }