90

既存のファイル パスから Bitmap または Drawable を作成しようとしています。

String path = intent.getStringExtra("FilePath");
BitmapFactory.Options option = new BitmapFactory.Options();
option.inPreferredConfig = Bitmap.Config.ARGB_8888;

mImg.setImageBitmap(BitmapFactory.decodeFile(path));
// mImg.setImageBitmap(BitmapFactory.decodeFile(path, option));
// mImg.setImageDrawable(Drawable.createFromPath(path));
mImg.setVisibility(View.VISIBLE);
mText.setText(path);

しかしsetImageBitmap()setImageDrawable()パスからの画像は表示されません。でパスを印刷しましmTextたが、次のようになります。/storage/sdcard0/DCIM/100LGDSC/CAM00001.jpg

私は何を間違っていますか?誰でも私を助けることができますか?

4

7 に答える 7

46

ここに解決策があります:

Bitmap bitmap = BitmapFactory.decodeFile(filePath);
于 2016-04-07T22:40:02.780 に答える
3

まあ、静的を使用するDrawable.createFromPath(String pathName)ことは、自分でデコードするよりも少し簡単に思えます... :-)

あなたmImgのが単純な場合は、ImageViewそれさえ必要ないので、mImg.setImageUri(Uri uri)直接使用してください。

于 2015-01-16T22:03:04.830 に答える
0

パスを介してドローアブルにアクセスすることはできないため、プログラムで構築できるドローアブルとの人間が読めるインターフェイスが必要な場合。

クラスのどこかで HashMap を宣言します。

private static HashMap<String, Integer> images = null;

//Then initialize it in your constructor:

public myClass() {
  if (images == null) {
    images = new HashMap<String, Integer>();
    images.put("Human1Arm", R.drawable.human_one_arm);
    // for all your images - don't worry, this is really fast and will only happen once
  }
}

今すぐアクセス -

String drawable = "wrench";
// fill in this value however you want, but in the end you want Human1Arm etc
// access is fast and easy:
Bitmap wrench = BitmapFactory.decodeResource(getResources(), images.get(drawable));
canvas.drawColor(Color .BLACK);
Log.d("OLOLOLO",Integer.toString(wrench.getHeight()));
canvas.drawBitmap(wrench, left, top, null);
于 2014-08-30T17:14:53.807 に答える