从SD卡android解码文件以避免由于大位图或setImageURI导致的内存不足错误
避免从SD卡或资源中选择大型图像文件时出现内存不足错误。 我该如何解决这个问题?
为避免从位图中选择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;
}
您可以使用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
并且此代码将为您找到最好的inSampleSize
值。
它对我很好。
如果你不想使用上面的代码,你也可以使用bitmap.recycle()
和System.gc()
来释放未使用的内存。但是,上面的代码对我来说工作正常。 你可以使用两者中的任何一个。
objbitmap.recycle();
objbitmap = null;
System.gc();
希望这可以解决问题!
这取决于你是否知道你想要的尺寸或不是。
如果你知道尺寸:
查看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.
}
}
链接地址: http://www.djcxy.com/p/16625.html
上一篇: Decode File From SdCard android to avoid out of memory error due to large bitmap or setImageURI