90

我正在尝试从现有文件路径创建位图或 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

155

从文件路径创建位图:

File sd = Environment.getExternalStorageDirectory();
File image = new File(sd+filePath, imageName);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
imageView.setImageBitmap(bitmap);

如果要将位图缩放到父级的高度和宽度,请使用Bitmap.createScaledBitmap函数。

我认为您提供了错误的文件路径。:) 希望这可以帮助。

于 2015-02-05T18:58:01.603 回答
64

这个对我有用:

File imgFile = new  File("/sdcard/Images/test_image.jpg");
if(imgFile.exists()){
    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
    //Drawable d = new BitmapDrawable(getResources(), myBitmap);
    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
    myImage.setImageBitmap(myBitmap);

}

编辑:

如果上述硬编码的 sdcard 目录在您的情况下不起作用,您可以获取 sdcard 路径:

String sdcardPath = Environment.getExternalStorageDirectory().toString();
File imgFile = new  File(sdcardPath);
于 2013-05-29T02:17:01.287 回答
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 回答
1
static ArrayList< Drawable>  d;
d = new ArrayList<Drawable>();
for(int i=0;i<MainActivity.FilePathStrings1.size();i++) {
  myDrawable =  Drawable.createFromPath(MainActivity.FilePathStrings1.get(i));
  d.add(myDrawable);
}
于 2015-04-17T07:09:46.420 回答
1
对于可绘制 -
Drawable drawable = Drawable.createFromPath(your path in string);
对于位图 -
Bitmap bitmap = BitmapFactory.decodeFile(your path in string);

多么简单希望你喜欢

于 2021-04-03T14:09:16.693 回答
0

你不能通过路径访问你的drawables,所以如果你想要一个带有你可以通过编程方式构建的drawables的人类可读界面。

在你的类的某处声明一个 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 回答