2013-02-02 40 views

回答

1

1)规模,并降低图像

/** 
* decodes image and scales it to reduce memory consumption 
* 
* @param file 
* @param requiredSize 
* @return 
*/ 
public static Bitmap decodeFile(File file, int requiredSize) { 
    try { 

     // Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(file), null, o); 

     // The new size we want to scale to 

     // Find the correct scale value. It should be the power of 2. 
     int width_tmp = o.outWidth, height_tmp = o.outHeight; 
     int scale = 1; 
     while (true) { 
      if (width_tmp/2 < requiredSize 
        || height_tmp/2 < requiredSize) 
       break; 
      width_tmp /= 2; 
      height_tmp /= 2; 
      scale *= 2; 
     } 

     // Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 

     Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(file), 
       null, o2); 

     return bmp; 

    } catch (FileNotFoundException e) { 
    } finally { 
    } 
    return null; 
} 

2)使用bitmap.Recycle();

3)使用System.gc();的大小指示的虚拟机,这将是一个很好的时间运行垃圾回收器

+0

另外,用DDMS发现内存分配。不要一直重新创建新的变量。但调整位图大小可能会诀窍! – psykhi