安卓图片调整大小时出现内存错误

5

我有一个旋转图像的方法,但我总是收到OutMemoryError错误,但我的图库中的图像是从相机拍摄的,大小宽度为5000〜来自手机

我将照片调整为宽度1280和高度960

我用的第一种显示和调整图像的方法是

    public static Boolean ShowImagesCapture(Context context, Uri PATH_IMAGE, ImageCropView view,int width, int height){

    int orientation=0;
    Boolean success = true;
    try {
        Bitmap bitmap =null;

        if (Build.VERSION.SDK_INT < 19) {
            String selectedImagePath = getPath(PATH_IMAGE,context);
            bitmap = BitmapFactory.decodeFile(selectedImagePath);
            orientation=GetPhotoOrientation(context,getRealPathFromURI(context,PATH_IMAGE));
        }

        else {
            ParcelFileDescriptor parcelFileDescriptor;

            try {
                parcelFileDescriptor = context.getContentResolver().openFileDescriptor(PATH_IMAGE, "r");
                FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
                bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
                parcelFileDescriptor.close();
                orientation=GetPhotoOrientation(context,getRealPathFromURI(context,PATH_IMAGE));

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        switch (orientation) {

            case ExifInterface.ORIENTATION_ROTATE_180:
                bitmap=rotateBitmap(bitmap,3,width,height);
                view.setImageBitmap(bitmap);

                break;
      break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                bitmap=rotateBitmap(bitmap,8,width,height);
                view.setImageBitmap(bitmap);
                break;

            case ExifInterface.ORIENTATION_TRANSVERSE:
                break;

            case ExifInterface.ORIENTATION_ROTATE_270:
                bitmap=rotateBitmap(bitmap,6,width,height);
                view.setImageBitmap(bitmap);
                break;

            default:
                view.setImageBitmap(bitmap);

        }

        bitmap = null;

    }
    catch (Exception e) {
        e.printStackTrace();
        success= false;
    }
    System.gc();
    return success;
}

我的方法是旋转图片
public static Bitmap rotateBitmap(Bitmap bitmap, int orientation,int width,int height) {

    try {
        Matrix matrix = new Matrix();
        switch (orientation) {
            case ExifInterface.ORIENTATION_NORMAL:
                return bitmap;
            case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
                //                matrix.setScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.setRotate(180);
                break;
            case ExifInterface.ORIENTATION_FLIP_VERTICAL:
                matrix.setRotate(180);
                //                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_TRANSPOSE:
                matrix.setRotate(90);
                //                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                matrix.setRotate(90);
                break;
            case ExifInterface.ORIENTATION_TRANSVERSE:
                matrix.setRotate(-90);
                //                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                matrix.setRotate(-270);
                break;
            default:
                return bitmap;
        }



        Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);
        bitmap.recycle();
        return bmRotated;

    }
    catch (OutOfMemoryError e) {
        Log.e(TAG,"Out memory Error");
        return null;
    }catch (Exception e){
        e.printStackTrace();
        return null;
    }
}

我的错误在哪里?

*-------------------**更新于2016年6月27日**-------------------*

我代码的最佳版本已经正常工作。

public static Bitmap rotateBitmap(Bitmap bitmap, int orientation,int width,int height) {

    try {
        Matrix matrix = new Matrix();
        switch (orientation) {
            case ExifInterface.ORIENTATION_NORMAL:
                return bitmap;
            case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
                matrix.setScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.setRotate(180);
                break;
            case ExifInterface.ORIENTATION_FLIP_VERTICAL:
                matrix.setRotate(180);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_TRANSPOSE:
                matrix.setRotate(90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                matrix.setRotate(90);
                break;
            case ExifInterface.ORIENTATION_TRANSVERSE:
                matrix.setRotate(-90);
                matrix.postScale(-1, 1);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                matrix.setRotate(-270);
                break;
            default:
                return bitmap;
        }
        Bitmap bmRotated= null;
        try {
            Bitmap tmp_bitmap= Bitmap.createScaledBitmap(bitmap,width,height,true);

            bmRotated = Bitmap.createBitmap(tmp_bitmap, 0, 0, tmp_bitmap.getWidth(),tmp_bitmap.getHeight(), matrix, true);

            bitmap.recycle();
        }catch (OutOfMemoryError e){
            e.printStackTrace();
        }
        return bmRotated;

    } catch (Exception e){
        e.printStackTrace();
        return null;
    }
}


 public static Bitmap decodefilebitmap(String selectedImagePath, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(selectedImagePath, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(selectedImagePath, options);
}

public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

//METODO PARA MOSTRAR LA IMAGEN DESDE LA GALERIA
public static Boolean ShowImagesCapture(Context context, Uri PATH_IMAGE, ImageCropView view,int width, int height){

    int orientation=0;
    Boolean success = true;
    try {
        Bitmap bitmap =null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        if (Build.VERSION.SDK_INT < 19) {
            String selectedImagePath = getPath(PATH_IMAGE,context);
            bitmap = decodefilebitmap(selectedImagePath,bitmap.getWidth(),bitmap.getHeight());
            orientation=GetPhotoOrientation(context,getRealPathFromURI(context,PATH_IMAGE));
        }

        else {
            ParcelFileDescriptor parcelFileDescriptor;

            try {
                parcelFileDescriptor = context.getContentResolver().openFileDescriptor(PATH_IMAGE, "r");
                FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
                bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
                parcelFileDescriptor.close();
                orientation=GetPhotoOrientation(context,getRealPathFromURI(context,PATH_IMAGE));

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        switch (orientation) {


            case ExifInterface.ORIENTATION_ROTATE_180:
                bitmap=rotateBitmap(bitmap,3,width,height);
                view.setImageBitmap(bitmap);

                break;

            case ExifInterface.ORIENTATION_ROTATE_90:
                bitmap=rotateBitmap(bitmap,8,width,height);
                view.setImageBitmap(bitmap);
                break;

            case ExifInterface.ORIENTATION_TRANSVERSE:
                break;

            case ExifInterface.ORIENTATION_ROTATE_270:
                bitmap=rotateBitmap(bitmap,6,width,height);
                view.setImageBitmap(bitmap);
                break;

            default:
                view.setImageBitmap(bitmap);

        }

        bitmap = null;

    }
    catch (Exception e) {
        e.printStackTrace();
        success= false;
    }
    System.gc();
    return success;
}
1个回答

3
那是因为您正在将整个位图加载到内存中bitmap = BitmapFactory.decodeFile(selectedImagePath);,然后显示一个调整大小的版本到ImageView(但浪费了内存,因为您在RAM中拥有完整大小的版本)。 您需要加载缩小的版本。 载入整个位图,然后对其进行操作(缩放,旋转,放入图像视图)与载入该位图的缩小版本不同。 例如,如果您有一个5000 x 5000像素的图像,在JPEG格式中其大小约为1MB。 但是,当您将其加载到内存中时,您会解压缩它并加载该图像的整个未压缩版本。 假设您以每像素32位为加载,则其在RAM中的大小将为5000x5000x32位,约为95MB! 因此,您需要加载缩小的版本。 参考此Android开发人员文档关于将缩小版本的位图载入内存。 这将有助于更好地理解问题。 您还可以使用像Glide这样的图像加载库。 这些库做所有这些以及更多。

是的,先使用resize方法调整低宽度和高度的图像大小,然后再旋转图像。谢谢! - pedroooo
@pedroooo 没问题,很高兴能帮到你 =)。 - josemgu91

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接