1

IDを使わずにimageviewに画像を表示したい。

すべての画像を raw フォルダーに配置して開きます

     try {
            String ss = "res/raw/images/inrax/3150-MCM.jpg";
             in = new FileInputStream(ss);
        buf = new BufferedInputStream(in);
        Bitmap bMap = BitmapFactory.decodeStream(buf);
        image.setImageBitmap(bMap);
        if (in != null) {
         in.close();
        }
        if (buf != null) {
         buf.close();
        }
    } catch (Exception e) {
        Log.e("Error reading file", e.toString());
    }

しかし、これは機能していません名前ではなくパスを使用して画像にアクセスしたい

4

3 に答える 3

1

openRawResource()を使用してバイトのストリームを読み取ります

このようなものはうまくいくはずです

InputStream is = context.getResources().openRawResource(R.raw.urfilename);

このリンクを確認してください

http://developer.android.com/guide/topics/resources/accessing-resources.html#ResourcesFromCode

それははっきりと次のように言っています

まれですが、元のファイルとディレクトリにアクセスする必要がある場合があります。その場合、ファイルをres /に保存しても機能しません。これは、res/からリソースを読み取る唯一の方法はリソースIDを使用することであるためです。

urコードに記載されているようなファイル名を付けたい場合は、おそらくアセットフォルダに保存する必要があります。

于 2010-09-11T14:29:06.750 に答える
1

try { // AssetManager への参照を取得
AssetManager mngr = getAssets();

        // Create an input stream to read from the asset folder
        InputStream ins = mngr.open(imdir);

        // Convert the input stream into a bitmap
        img = BitmapFactory.decodeStream(ins);

  } catch (final IOException e) {
        e.printStackTrace();
  } 

ここで画像ディレクトリはアセットのパスです

お気に入り

assest -> image -> somefolder -> some.jpg

その後、パスは

画像/somefolder/some.jpg

image のリソース ID は不要になりました。これを使用して実行時にイメージを設定できます。

于 2010-09-11T23:02:03.377 に答える
1

Resources.getIdentifier(name, type, package)rawファイルで使用できる場合があります。これでIDが取得され、setImageResource(id)などを続行できます。

int id = getResources().getIdentifier("3150-MCM", "raw", getPackageName());
if (id != 0) //if it's zero then its not valid
   image.setImageResource(id);

あなたが欲しいものですか?複数のフォルダが気に入らないかもしれませんが、試してみる価値はあります。

于 2010-09-11T20:44:45.203 に答える