我想在屏幕上显示图像(我有一个显示图像的 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/