从文件路径创建Bitmap/Drawable

97

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

我做错了什么?有人可以帮我吗?


BitmapFactory.decodeFile(path) --> 这个方法会为您返回一个Bitmap对象吗?您能够验证一下吗? - toantran
1
@autobot_101 在调试模式下,它的 mBuffer 中有 id。但是它的 mHeightmWidth 值为 -1,而且 mLayoutBoundsnull - Nari Kim Shin
那么您应该再次检查文件路径,因为这意味着您的图像尚未“解压”为位图对象。也许您可以尝试另一个图像。 - toantran
1
@autobot_101 实际上,我是从“Cursor”获取了这个图像路径,并尝试了其他图像,但结果相同。此外,我通过“adb shell”检查了路径,并发现图像存在于该路径中。 - Nari Kim Shin
7个回答

159

从文件路径创建位图:

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函数。

我认为您提供了错误的文件路径。


1
我早就得到了我的解决方案,但我会把这个作为正确答案,因为它还可以处理在加载大规模图像时出现的OOM错误。非常干净和好的解决方案!谢谢! - Nari Kim Shin
1
我假设这里的imageName是任何字符串?还是特定的imageName? - Jeet
@JeetendraChoudhary 是的,imageName 可以是任何最终字符串作为图像的名称。 - CodeShadow

71

对我来说有效:

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

}

编辑:

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

String sdcardPath = Environment.getExternalStorageDirectory().toString();
File imgFile = new  File(sdcardPath);

3
尝试从Environment.getExternalStorageDirectory().toString()获取SdCard路径,然后尝试。 - Antarix

50

这里有一个解决方案:

Bitmap bitmap = BitmapFactory.decodeFile(filePath);

我正在寻找它! - Abner Escócio

3

使用静态方法Drawable.createFromPath(String pathName)似乎比自己解码更为直接简单... :-)

如果你的mImg是一个简单的ImageView,你甚至不需要它,直接使用mImg.setImageUri(Uri uri)就可以了。


2
对于Drawable -
Drawable drawable = Drawable.createFromPath(your path in string);

对于位图 -
Bitmap bitmap = BitmapFactory.decodeFile(your path in string);

多简单希望你喜欢


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

4
请注意,您提供的代码需要在回答中进行解释。 - Daniel Nugent

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接