我试图修复Android Camera Intent Saving Image Landscape When Taken Portrait的问题,但遇到了dalvikvm-heap Out of memory on a 63489040-byte allocation.
错误的问题。我看了createBitmap() leads me into a java.lang.OutOfMemoryError,但这个问题没有任何帮助。我不知道如何解决这个问题。我尝试在位图上调用recycle()
,但这没有用。
String file = getRealPathFromURI(Uri.parse(mUriString));
BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file, bounds);
BitmapFactory.Options opts = new BitmapFactory.Options();
Bitmap bm = BitmapFactory.decodeFile(file, opts);
ExifInterface exif = new ExifInterface(file);
String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
int orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL;
int rotationAngle = 0;
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotationAngle = 90;
case ExifInterface.ORIENTATION_ROTATE_180:
rotationAngle = 180;
case ExifInterface.ORIENTATION_ROTATE_270:
rotationAngle = 270;
}
Matrix matrix = new Matrix();
matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true);
bm.recycle();
此代码读取EXIF数据并正确定位图像。
我还应该补充说我在这里压缩图像:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
rotatedBitmap.recycle();
baos.close();
答案 0 :(得分:17)
您可以通过跳过BitmapFactory.Options
并使用ARGB_8888
来解码RGB_565
中的图片,从而减少内存。和inDither
为真,以保持图像质量。
<强>样品:强>
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = false;
opts.inPreferredConfig = Config.RGB_565;
opts.inDither = true;
答案 1 :(得分:3)
您可以使用opts.inSampleSize = 2 or above
来避免OOM
例外。