我的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;
}
}
};
有时我会让用户抱怨内存异常会被扔到手机上。我的设备从未遇到过这个问题,这可能与手机内存不足有关吗?
有人可以查看此代码并向我提供有关如何提高效率的提示吗? 谢谢!
答案 0 :(得分:3)
总是在这样的清单中尝试 android:largeHeap =“true”
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
>
答案 1 :(得分:0)
您可以将缩小版本加载到内存中
您需要查看Loading Large Bitmaps Efficiently开发者网站