避免从sdcard或资源中选择大型图像文件时出现内存不足错误。 我该如何解决这个问题?从SdCard解码文件android以避免由于大位图或setImageURI而导致的内存不足错误
2
A
回答
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
这取决于如果你知道你想要或不就结了什么尺寸。
如果你知道的大小:
见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
相关问题
- 1. Android ImageSwitcher:内存不足错误setImageURI
- 2. Android setImageURI内存不足错误
- 3. 是否会由于内存泄漏而导致内存不足?
- 4. Android位图内存不足错误
- 5. 内存不足位图解码文件 - Android
- 6. 如何处理大对象并避免内存不足错误?
- 7. 在Android中显示图像会导致内存不足错误
- 8. createBitmap导致内存不足错误
- 9. Spring,Java:流式文件下载以避免内存不足错误
- 10. 位图调整大小会导致内存不足
- 11. Android位图 - 内存不足
- 12. 如何删除充气异常由于内存不足导致的错误
- 13. 位图错误 - 内存不足
- 14. Android:避免由于未处理的错误导致应用程序崩溃
- 15. 如何避免delphi中的循环内存不足错误?
- 16. Android解压缩PNG,将导致潜在的内存不足错误
- 17. 无法完成由于内存不足导致的承诺
- 18. 如何忽略由于LibXML编码错误而导致的解析错误?
- 19. gzinflate:max。避免内存不足的空间?
- 20. Android Studio中的位图分配了内存不足的错误
- 21. Android中的位图内存不足错误...
- 22. 由于内存不足导致应用程序崩溃
- 23. 由于内存不足导致应用程序崩溃
- 24. Android。内存泄漏导致内存不足错误。使用查看传呼机。
- 25. 内存不足而导线Android的文件系统
- 26. 由于堆大小增加,在android中出现内存不足错误
- 27. 如何编写图像而不会导致Java内存不足?
- 28. 由于堆栈大小不足导致python的dll加载错误?
- 29. joomla内存不足致命错误
- 30. 加载大量的图像导致内存不足
欲了解更多信息,请使用以下文档:http://developer.android.com/training/displaying-bitmaps/load-bitmap.html – Mahesh 2013-03-13 04:38:40
这是为了防止图像大小像1200x800非常大,并且你想显示缩略图这个图像,如果你直接使用setImageURI或者直接从SD卡获得位图,你会得到outofmemory错误。 – Mahesh 2013-03-13 04:42:39
谢谢你,节省了我的时间。 – 2014-06-20 06:55:59