无法从SDCard检索图像

时间:2012-12-12 08:10:35

标签: android android-layout android-sdcard android-image

Drawable image = Drawable.createFromPath(newImagepath);
defective_image.setBackgroundDrawable(image);   

图像存储在newImagePath变量中,该变量是String。以上引发了outOfMemory Exception。

我也尝试将图像检索到Bitmap对象中。

Bitmap bitmapImg= BitmapFactory.decodeFile(newImagepath);

在上面的例子中,bitmapImg是Null。 (没有任何例外)

但是,如果将其作为文件检索,则表示成功。这不符合我的目的,因为我希望这个图像是RelativeLayout的背景。

File imageFile = new File(newImagepath);
String imgPath = imageFile.getAbsolutePath();

imgPath与newImagePath相同,总结路径没有错。我还验证了SDCard中图像的存在。

此图像是从设备的相机中捕获的。此代码正在成功处理模拟器。在设备上调试时,注意到上述故障。 我也尝试过: -

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

2 个答案:

答案 0 :(得分:0)

我宁愿使用BitmapFactory来解码文件路径中的图像:

Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
jpgView.setImageDrawable(bitmap);

答案 1 :(得分:0)

这似乎是一个记忆问题。所以我建议使用选项

解码位图大小
options.inJustDecodeBounds = true;

然后通过查看字段来获取此位图的大小(不要尝试使用位图:它将为null):

options.outWidth;
options.outHeight;

然后创建缩放位图(取决于屏幕尺寸)。为此,您可以查看

BitmapFactory.Options.inSampleSize