开发者

BitmapFactory.decodeFile works fine on emulator but fails on HTC desire

I don't understand the problem, why BitmapFactory.decodeFile works fine on emulator but fails on HTC desire. The following code:

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

It's simple, but the application stops unexpectedly on HTC desire. Oh, I find the proble开发者_StackOverflowm:

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

The images are too large, how to handle them? I need good quality images too.


The recommended way of using external path is getExternalStorageDirectory

Change your code to something like this.

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


Use a BitmapFactory.Options object while decoding the file. Set inSampleSize to something like 2 or 4 depending on your screen resolution to get an optimum preview. What it does is that it downsamples the image so that it does not take too much memory.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜