2013-03-13 77 views

回答

12

为了避免错误,而从SD卡中选择位图文件或使用setImageURI到SD卡使用下面的方法:

public static Bitmap decodeScaledBitmapFromSdCard(String filePath, 
     int reqWidth, int reqHeight) { 

    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(filePath, options); 

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeFile(filePath, options); 
} 

public static int calculateInSampleSize(
     BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 

     // Calculate ratios of height and width to requested height and width 
     final int heightRatio = Math.round((float) height/(float) reqHeight); 
     final int widthRatio = Math.round((float) width/(float) reqWidth); 

     // Choose the smallest ratio as inSampleSize value, this will guarantee 
     // a final image with both dimensions larger than or equal to the 
     // requested height and width. 
     inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; 
    } 

    return inSampleSize; 
} 
+2

欲了解更多信息,请使用以下文档:http://developer.android.com/training/displaying-bitmaps/load-bitmap.html – Mahesh 2013-03-13 04:38:40

+0

这是为了防止图像大小像1200x800非常大,并且你想显示缩略图这个图像,如果你直接使用setImageURI或者直接从SD卡获得位图,你会得到outofmemory错误。 – Mahesh 2013-03-13 04:42:39

+1

谢谢你,节省了我的时间。 – 2014-06-20 06:55:59

2

这取决于如果你知道你想要或不就结了什么尺寸。

如果你知道的大小:

见Android_Craker答案。这是解决问题的坚实途径。

如果您不知道大小:

在你想要的形象大到将适合存储这种情况下。解决方案是循环缩减采样,直到找到适合的大小: BitmapFactory.Options options = new BitmapFactory.Options();

boolean done = false; 
int downsampleBy = 1; 
Bitmap bitmap = null; 
while (!done) { 
    options.inSampleSize = downsampleBy++; 
    try { 
     bitmap = BitmapFactory.decodeFile(filePath, options); 
     done = true; 
    catch (OutOfMemoryError e) { 
     // Ignore. Try again. 
    } 
} 
3

您可以使用inSampleSize来减少内存占用。

这里是代码

public static Bitmap decodeAndResizeFile(File f) { 
    try { 
     // Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(f), null, o); 

     // The new size we want to scale to 
     final int REQUIRED_SIZE = 70; 

     // 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 < REQUIRED_SIZE || height_tmp/2 < REQUIRED_SIZE) 
       break; 
      width_tmp /= 2; 
      height_tmp /= 2; 
      scale *= 2; 
     } 

     // Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
    } catch (FileNotFoundException e) { 
    } 
    return null; 
} 

在这里,它会使用inSampleSize解码图像和此代码将找到最好的inSampleSize值你。

它对我很好。

如果您不想使用上面的代码,您还可以使用bitmap.recycle()System.gc()来释放未使用的内存。但是,上面的一个对我来说工作正常。你可以使用两者中的任何一个。

objbitmap.recycle(); 
objbitmap = null; 
System.gc(); 

希望这可以解决问题!

+0

谢谢我试过它的工作正常。 – 2016-02-04 09:52:00

相关问题