Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
836 views
in Technique[技术] by (71.8m points)

how to compress image for imageview in android

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...