java如何避免与BitmapFactory不一致的OutOfMemory异常。德比提雷?
我的安卓应用程序有以下代码:
private PictureCallback pictureTakenCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bmp = null;
Bitmap scaledBitmap = null;
ByteArrayOutputStream baos = null;
showLoading(true);//UI crap
try
{
bmp = BitmapFactory.decodeByteArray(data, 0, data.length); //OOM Exception Thrown Here
//if the bitmap is smaller than 1600 wide, scale it up while preserving aspect ratio
if(bmp.getWidth() < 1600) {
int originalHeight = bmp.getHeight();
int originalWidth = bmp.getWidth();
scaledBitmap = Bitmap.createScaledBitmap(bmp, 1600,
originalHeight*1600/originalWidth, true);
bmp = scaledBitmap;
scaledBitmap = null;
}
baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 30, baos); // 30% compression
image = baos.toByteArray();
submitImage();
}
catch (java.lang.OutOfMemoryError e) {
e.printStackTrace();
showLoading(false);
}
catch (Exception e) {
e.printStackTrace();
showLoading(false);
}
finally
{
bmp = null;
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
baos = null;
}
}
};
有时我会让用户抱怨他们的手机内存不足。我的设备从来没有遇到过这个问题,这可能与他们的手机内存不足有关吗
有人能看一下这段代码,给我一些如何提高效率的建议吗? 谢谢
# 1 楼答案
您可以将缩小的版本加载到内存中
你需要看看Loading Large Bitmaps Efficiently开发者网站