73

プロジェクト関連の画像をドローアブルフォルダに保存しています。また、画像名を文字列変数に格納しており、それらの画像をimageviewに動的に設定しようとしています。しかし、画像は表示されていません。この点で私を助けてください。

私のコード:

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

上記のコードでは、「imagename」は画像名を含む文字列変数です。

前もって感謝します

4

17 に答える 17

132

これを試して:

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);
于 2012-07-31T10:08:47.987 に答える
43

ここで私はfrnd_inactive画像をからdrawableの画像に設定しています

 imageview= (ImageView)findViewById(R.id.imageView);
 imageview.setImageDrawable(getResources().getDrawable(R.drawable.frnd_inactive));
于 2012-07-31T10:03:53.447 に答える
36
imageview= (ImageView)findViewById(R.id.imageView);
imageview.setImageResource(R.drawable.mydrawable);
于 2012-07-31T10:03:56.540 に答える
21

この動的コードを試してください

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オブジェクトが必要になります。

于 2012-07-31T10:11:03.380 に答える
8

以下のコードを参照してください。これは私のために働いています

iv.setImageResource(getResources().getIdentifier(
                "imagename", "drawable", "com.package.application"));
于 2012-07-31T10:06:15.270 に答える
7

これは私にとってはうまくいきます(画像の拡張子を使用しないでください。名前だけです):

String imagename = "myImage";
int res = getResources().getIdentifier(imagename, "drawable", this.getPackageName());
imageview= (ImageView)findViewById(R.id.imageView);
imageview.setImageResource(res);
于 2014-06-22T02:59:36.430 に答える
6

これが、今日のすべての電話で機能する最良の方法です。これらの回答のほとんどは非推奨であるか、API >= 19 または 22 が必要です。必要なものに応じて、フラグメント コードまたはアクティビティ コードを使用できます。

断片

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

アクティビティ

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

乾杯!

于 2016-12-14T18:03:50.740 に答える
3
imageView.setImageResource(R.drawable.picname);
于 2015-05-06T10:10:28.850 に答える
3

getDrawable()非推奨なので、これを試してください

imageView.setImageResource(R.drawable.fileName)
于 2016-11-18T12:00:59.507 に答える
2

まず、あなたの画像名はmyimageです。したがって、あなたがしなければならないのは、Drawableに移動して画像名myimageを保存することです。

ここで、画像名しか知らず、それにアクセスする必要があると仮定します。以下のスニペットを使用してアクセスし、

あなたがしたことは正しいです、あなたがコーディングの中で使用しようとしている画像名を保存したことを確認してください。

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

それに加えて、空きスペースや大文字小文字の区別がないことを確認する必要があります

于 2012-07-31T10:25:09.763 に答える
2

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);
于 2016-08-28T20:08:49.673 に答える
1

getDrawable() は非推奨です。 今、あなたは使用することができます

imageView.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.msg_status_client_read)) 
于 2016-09-27T06:59:12.207 に答える
0

これは私にとってはうまくいきます。

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]);
}
于 2015-07-27T10:10:39.823 に答える
0
ImageView imageView=findViewById(R.id.imageView)

描画可能なフォルダーに画像がある場合は、使用します

imageView.setImageResource(R.drawable.imageView)

uriがあり、それをimageViewに表示したい場合は、使用します

imageView.setImageUri("uri")

ビットマップがあり、それをimageViewに表示したい場合は、使用します

imageView.setImageBitmap(bitmap)

注:- 1. imageView.setImageDrawable()Java で非推奨になりました 2. 画像 uri が firebase または他のオンライン リンクからのものである場合は、使用します

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

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

または使用

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

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

于 2020-08-16T07:15:58.380 に答える