java - Android 尝试旋转图像时出现 OutOfMemoryError

标签 java android bitmap

我想在屏幕上显示图像(我有一个显示图像的 Activity ):

Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
ExifInterface exif = new ExifInterface(imagePath);
int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int rotationInDegrees = exifToDegrees(rotation);
Matrix matrix = new Matrix();
matrix.preRotate(rotationInDegrees);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

最后一行抛出 java.lang.OutOfMemoryError

我不明白该怎么做...我必须首先创建位图(BitmapFactory.decodeFile),但是当我想旋转它时,我必须已经提供源位图 - 如何避免它?

谢谢

最佳答案

您可以在位图选项中添加 inSampleSize 来尝试先缩小图像尺寸,然后再进行解码。如文件所述:http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize

If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory.

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 5;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);

希望有帮助。

关于java - Android 尝试旋转图像时出现 OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27196046/

相关文章:

java - 抽象类和动态绑定(bind)

java - 本地时间有问题

android 系统尝试绘制大尺寸位图(崩溃报告)

java - Android - 文件不会 move 或复制到内部应用程序存储

java - FXML 文件选择器无法重新打开

android - Kotlin 在字符串列表中查找子字符串

android - android中 ImageView 的图像重叠

Android:自定义 View 创建教程

Android:复制位图

c++ - 如何保存24位BMP