lsc*_*ger 2 java memory android image bitmap
我试图修复Android摄像头意图保存图像景观时拍摄肖像问题,但遇到了dalvikvm-heap Out of memory on a 63489040-byte allocation.错误的问题.我看了一下createBitmap()引导我进入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();
Run Code Online (Sandbox Code Playgroud)
此代码读取EXIF数据并正确定位图像.
我还要补充一点,我在这里压缩图像:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
rotatedBitmap.recycle();
baos.close();
Run Code Online (Sandbox Code Playgroud)
Rod*_*uin 18
在BitmapFactory.Options通过跳过解码图像ARGB_8888并使用时,可以减少内存RGB_565.并inDither保持图像质量.
样品:
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = false;
opts.inPreferredConfig = Config.RGB_565;
opts.inDither = true;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9309 次 |
| 最近记录: |