createbitmap上的内存不足错误

时间:2016-03-23 12:25:49

标签: android image

                    BitmapFactory.Options bounds = new BitmapFactory.Options();
                    bounds.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(Common.getRealPathFromURI(selectedImage,rootView.getContext()), bounds);
                    BitmapFactory.Options opts = new BitmapFactory.Options();
                    Common.setBitmap(null);
                    Bitmap bm = BitmapFactory.decodeFile(Common.getRealPathFromURI(selectedImage, rootView.getContext()), opts);
                    saveToInternalSorage(bm);
                    ExifInterface exif = new ExifInterface(Common.getRealPathFromURI(selectedImage,rootView.getContext()));
                    String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
                    int orientation = orientString != null ? Integer.parseInt(orientString) :  ExifInterface.ORIENTATION_NORMAL;
                    int rotationAngle = 0;
                    if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90;
                    if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180;
                    if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270;
                    Matrix matrix = new Matrix();
                    matrix.postRotate(rotationAngle);
                    try
                    {                       
                        Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
                        bitmap2 = rotatedBitmap;//BitmapFactory.decodeStream(imageStream);
                    }
                    catch(OutOfMemoryError e)
                    {                       
                    }

基本上有时候,我的图像会旋转。所以我已经实现了这个功能,问题在于createbitmap我得到了outofmemory错误。我想避免这个错误,而不是修改图像的大小......和质量。我想保持相同的尺寸和质量。

2 个答案:

答案 0 :(得分:0)

如果要使用大堆,则应使用AndroidManifest.xml编写。

机器人:largeHeap = “真”

如果要检查设备上的堆大小,请调用ActivityManager.getLargeMemoryClass()。

ActivityManager am =((ActivityManager)getSystemService(Activity.ACTIVITY_SERVICE)); int largeMemory = am.getLargeMemoryClass();

答案 1 :(得分:0)

您遇到了更大的问题,例如您的应用无法在大多数Android设备上运行。 Use your Uri properly并摆脱getRealPathFromURI()

话虽如此,但无法保证您可以分配旋转图像所需大小的位图。是否可以基于堆的状态和图像的分辨率。您的选择是:

  1. 切换到通过NDK使用本机代码进行图像旋转。这也不能保证工作,但可能性要高得多,因为你可以使用所有系统RAM。

  2. 切换为使用android:largeHeap="true"。这对许多设备没有影响。但是,在某些情况下,它会增加您的堆限制,并使您的位图分配更有可能成功。

  3. 用于调整图像大小。

  4. 告诉用户"抱歉,我们没有足够的内存来旋转图像"。

相关问题