BitmapFactory.decodeFile在模拟器上工作正常但在HTC欲望上失败

时间:2011-07-18 06:40:19

标签: android bitmap android-emulator decode

我不明白这个问题,为什么BitmapFactory.decodeFile在模拟器上工作正常,但在HTC欲望上失败。以下代码:

File f = new File("/sdcard/DCIM/100MEDIA/");
File files[] = f.listFiles();

Bitmap b1 = null, b2 = null;
b1 = BitmapFactory.decodeFile(files[0].getPath());
b2 = BitmapFactory.decodeFile(files[1].getPath());

这很简单,但应用程序意外停止了HTC的愿望。 哦,我发现了问题:

07-18 08:01:45.634: ERROR/dalvikvm-heap(20637): 8045568-byte external allocation too large for this process.
07-18 08:01:45.634: ERROR/(20637): VM won't let us allocate 8045568 bytes
07-18 08:01:45.743: ERROR/AndroidRuntime(20637): java.lang.OutOfMemoryError: bitmap size exceeds VM budget

图像太大,如何处理?我也需要高质量的图像。

2 个答案:

答案 0 :(得分:2)

使用外部路径的推荐方法是getExternalStorageDirectory

将您的代码更改为此类内容。

File f = new File(Environment.getExternalStorageDirectory() + "/DCIM/100MEDIA/");

答案 1 :(得分:0)

解码文件时使用BitmapFactory.Options对象。根据您的屏幕分辨率,将inSampleSize设置为2或4,以获得最佳预览。它的作用是对图像进行下采样,使其不会占用太多内存。

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 2;
Bitmap b1 = BitmapFactory.decodeFile(files[0].getPath(), opts);;
相关问题