如何设置图像从可绘制的动态在机器人?

我存储我的项目相关的图像在可绘制的文件夹。此外,我存储在字符串变量的图像名称,并动态我试图设置这些图像的图像视图。但是图像没有显示。请在这方面帮助我。

My Code:

int res = getResources().getIdentifier(imagename, "drawable", this.getPackageName());
imageview= (ImageView)findViewById(R.id.imageView);
imageview.setImageResource(res);

在上面的代码中,“ imagename”是包含图像名称的字符串变量。

先谢谢你

232492 次浏览

在这里,我设置的 frnd_inactive图像从 drawable的图像

 imageview= (ImageView)findViewById(R.id.imageView);
imageview.setImageDrawable(getResources().getDrawable(R.drawable.frnd_inactive));
imageview= (ImageView)findViewById(R.id.imageView);
imageview.setImageResource(R.drawable.mydrawable);

See below code this is working for me

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

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

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

在上面的代码中,您将需要 Image-file-Name 和 Image-View 对象 你们两个都有

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.

现在假设您只知道图像名称,并且需要访问它,

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

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);
imageView.setImageResource(R.drawable.picname);

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

从 API 22开始,不推荐使用 getResources().getDrawable()(另请参阅 Android getResources().getDrawable() deprecated 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);

getDrawable() is deprecated. 现在可以使用

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

getDrawable()已被弃用,所以请尝试这样做

imageView.setImageResource(R.drawable.fileName)

这是 今天每部手机都能用最好的方法。大多数这样的答案都是不推荐的,或者需要 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);

干杯!

int[] phptr=new int[5];

向数组添加图像指针

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]);
}
ImageView imageView=findViewById(R.id.imageView)

如果您有图像在可绘制的文件夹,然后使用

imageView.setImageResource(R.drawable.imageView)

如果您有 uri 并且想要在 image View 中显示它,那么使用

imageView.setImageUri("uri")

if you have bitmap and want to display it in imageView then use

imageView.setImageBitmap(bitmap)

注意: -1。现在在 java 中不推荐使用 imageView.setImageDrawable() 2. If image uri is from firebase or from any other online link then use

    Picasso.get()
.load("uri")
.into(imageView)

(https://github.com/square/picasso)

或使用

Glide.with(context)
.load("uri")
.into(imageView)

(https://github.com/bumptech/glide)

Here is how to do it in 科特林, inside a loop i:

Code reused from rfsbraz 's answer

    val uri = "@drawable/pic_top${i+1}"
val imageResource = activity.resources.getIdentifier(uri, null, activity.packageName)


val res = activity.getResources().getDrawable(imageResource)
holder.view.imgDigit.setImageDrawable(res)

set in ImageView like this : imageView.setImageResource(R.drawable.image)