AraZZ
AraZZ

Reputation: 167

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 problem:

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.

Upvotes: 0

Views: 1231

Answers (2)

Abhinav
Abhinav

Reputation: 39932

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

Upvotes: 0

PravinCG
PravinCG

Reputation: 7708

The recommended way of using external path is getExternalStorageDirectory

Change your code to something like this.

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

Upvotes: 2

Related Questions