0

Hi I want to show 3 or 4 image in my view that are stored in sdcard the size of images is 1-2 MB approximately. My problem is when I use image in imageview then it throw out of memory exception i have create bit and pass option when decoding image to bitmap

02-26 13:16:54.946: ERROR/dalvikvm-heap(23410): 15980544-byte external allocation too large for this process.
02-26 13:16:54.946: ERROR/dalvikvm(23410): Out of memory: Heap Size=3407KB, Allocated=2801KB, Bitmap Size=15630KB, Limit=21884KB
02-26 13:16:54.946: ERROR/dalvikvm(23410): Trim info: Footprint=3463KB, Allowed Footprint=3655KB, Trimmed=248KB
02-26 13:16:54.946: ERROR/GraphicsJNI(23410): VM won't let us allocate 15980544 bytes
02-26 13:16:54.986: ERROR/AndroidRuntime(23410): FATAL EXCEPTION: main
02-26 13:16:54.986: ERROR/AndroidRuntime(23410): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
02-26 13:16:54.986: ERROR/AndroidRuntime(23410):     at android.graphics.BitmapFactory.nativeDecodeFile(Native Method)
02-26 13:16:54.986: ERROR/AndroidRuntime(23410):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:325)

can any body help me in solving my problem thanks in advance

EboMike
  • 76,846
  • 14
  • 164
  • 167
Jaiprakash Soni
  • 4,100
  • 5
  • 36
  • 67

2 Answers2

5

I have found solution of my problem there is my code :

BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[24*1024];  
options.inJustDecodeBounds = false;
options.inSampleSize=32;     
bmp1=BitmapFactory.decodeFile(filepath1,options);  
Bitmap b1=ThumbnailUtils.extractThumbnail(bmp1,30, 30);  
iv1.setImageBitmap(b);  
 if(bmp1!=null){  
   bmp1.recycle();
   }  
         bmp1=BitmapFactory.decodeFile(filepath1,options);
 Bitmap b2=ThumbnailUtils.extractThumbnail(bmp1,30, 30);  
 iv2.setImageBitmap(b2);  
if(bmp1!=null){  
 bmp1.recycle();
 }

similarly I have use it for four image view and set image without OOM Exception

Dharmendra
  • 33,296
  • 22
  • 86
  • 129
Jaiprakash Soni
  • 4,100
  • 5
  • 36
  • 67
1

Create a BitmapFactory.Options and pass a value >1 in inSampleSize (preferably a power of 2) to scale the image down when loading it.

EboMike
  • 76,846
  • 14
  • 164
  • 167
  • I have created option is BitmapFactory.Options options = new BitmapFactory.Options(); options.inTempStorage = new byte[16*1024]; options.inJustDecodeBounds = false; options.inSampleSize=8; – Jaiprakash Soni Feb 26 '11 at 08:45
  • Are you saying that this solved your problem, or are you out of memory despite that? – EboMike Feb 26 '11 at 15:25
  • @EboMike : thanx for helping me. I am saying that it don't solve my problem it still throw OOM exception when I have use BitmapFactory Option in my code. – Jaiprakash Soni Feb 28 '11 at 09:44
  • That seems weird. How big are the textures, and what value do you pass into inSampleSize? How many images are you loading until you run out of memory? – EboMike Feb 28 '11 at 09:47
  • @EboMike : The image is of approx 1.5 to 2 MB and I have first try with 4 into inSampleSize and after that i have try it with 8. here is snape of my code if(bmp1!=null){ bmp1.recycle(); } BitmapFactory.decodeFile(filepath,options); and option that i have used is shown above filepath is path to my image adn decodeFile() function throw OOM exception – Jaiprakash Soni Mar 01 '11 at 10:15
  • @lawat: My question was more about dimensions of the image, i.e. width and height. One more thing - are you testing on a debugger, or in the emulator? – EboMike Mar 01 '11 at 17:13
  • @EboMike : 1. the dimension of image is 3264*2448 that is captured by HTC desire HD but i have to show thumbnail of very small size but not to affect quality of original image that have taken by camera 2. I have not sound knowledge of debugger i have tested it on device and check logcat and i am sure that the line BitmapFactory.decodeFile(filepath,options); throw OOM exception and one more thing this work properly if i use just only one image but when i use two or more i got exception and i have to show 4 images. Is there any way of creating such thumbnail plese help me. thanx EboMike – Jaiprakash Soni Mar 05 '11 at 09:20
  • 1. A sample size of 8 will still result in 408x306 - that's larger than you need for a thumbnail. Did you try 16? 2. Okay, just checking. The debugger is known to leak memory when using bitmaps. 3. Are you SURE you're not leaking? Can you add debug output whenever you're decoding a bitmap? See how many you're decoding before you hit the OOM. – EboMike Mar 05 '11 at 11:17
  • I have try inSampleSize=64 but I got same exception when using two images in my application. can any body know how to get thumbnail of image from image path or how to handle OOM exception please help me ... – Jaiprakash Soni Mar 16 '11 at 09:09