【发布时间】:2014-12-05 04:01:25
【问题描述】:
我的 android 应用程序有以下代码:
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;
}
}
};
有时我让用户抱怨他们的手机出现内存不足异常。我的设备从来没有遇到过这个问题,这可能与他们的手机内存不足有关吗?
有人可以看看这段代码并给我一些提示,告诉我如何提高它的效率吗? 谢谢!
【问题讨论】:
标签: java android bitmapfactory