OOM解决办法

手动进行垃圾回收   调用bitmap.recycle() 或者System.gc();

If you use them for fit-in screen display only, just scale them to the 

screen-size. You don't need any bigger. 

1. Figure out the actual width and height of the image 
(inJustDecodeBounds = true) 
2. When actually loading the pic, downsample (inSampleSize > 1) so 
that the resulting image fits the screen as good as possible (for best 
result, make the loaded image a bit bigger than the screen instead of 
a bit smaller). Also, the values of inSampleSize work best across most 
phones when its value is a power of 2 (2, 4, 8, etc) 
3. Then scale the image (down) a bit further to exactly fit the 
screen. 
E.g. say your screen is 800x400. Your image is 2048x1536. Then 
downsample the image (inSampleSize = 2) --> Loaded image is 1024x768. 
Then scale the downsampled image by 52.08333% --> a bitmap of 533x400 
pixels. Cache this image. Still, be careful not to cache too many 
images. 
On Feb 8, 9:48 am, Samuh <samuh.va ...@gmail.com> wrote:
------------------------------------------------------------------------------------
  1. private Bitmap decodeFile(File f){
  2.     Bitmap b = null;
  3.     try {
  4.         //Decode image size
  5.         BitmapFactory.Options o = new BitmapFactory.Options();
  6.         o.inJustDecodeBounds = true;
  7.         BitmapFactory.decodeStream(new FileInputStream(f), null, o);
  8.         int scale = 1;
  9.         if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
  10.             scale = Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
  11.         }
  12.         //Decode with inSampleSize
  13.         BitmapFactory.Options o2 = new BitmapFactory.Options();
  14.         o2.inSampleSize = scale;
  15.         b = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
  16.     } catch (FileNotFoundException e) {
  17.     }
  18.     return b;
  19. }
复制代码


Reference : 
           1.Handling big Bitmaps                        http://groups.google.com/group/a ... 1a63ad2?lnk=gst&;q=samu
           2. 'bitmap size exceeds VM budget' if Activity is restarted [includes test demo!]
            http://code.google.com/p/android/issues/detail?id=8488
           3. How to... if you want to create and destroy the Bitmaps too frequently...

http://mobi-solutions.blogspot.c ... -to-create-and.html  
                     4.Handling large Bitmaps
            http://stackoverflow.com/questions/2220949/handling-large-bitmaps

猜你喜欢

转载自peirenlei.iteye.com/blog/1812355