【问题标题】:java.lang.IllegalStateException: Can't compress a recycled bitmapjava.lang.IllegalStateException:无法压缩回收的位图
【发布时间】:2019-08-03 18:02:00
【问题描述】:

我在捕获图像并从图库中获取图像时收到此错误。

W/System.err: java.lang.IllegalStateException: Can't compress a recycled bitmap
2019-03-13 11:32:10.429 4878-4878/com.judad.driver W/System.err:     at android.graphics.Bitmap.checkRecycled(Bitmap.java:395)
2019-03-13 11:32:10.429 4878-4878/com.judad.driver W/System.err:     at android.graphics.Bitmap.compress(Bitmap.java:1516)
2019-03-13 11:32:10.429 4878-4878/com.judad.driver W/System.err:     at com.example.abhishek.e_commerce.fragment.My_profile_edit_Fragment.commonImageToFile(My_profile_edit_Fragment.java:1377)

代码:

public  Bitmap rotateBitmap(Bitmap bitmap, int orientation) {

        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(-90);
                break;
            default:
                return bitmap;
        }
        try {
            Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            if (bitmap!=bmRotated){
                bitmap.recycle();
            }
            return bmRotated;
        }
        catch (OutOfMemoryError e) {
            e.printStackTrace();
            return null;
        }
    }
    private void commonImageToFile(Bitmap retrievedBitmap, int pos) {
        try {
            String timeStamp = new SimpleDateFormat("yyyMMdd_HHmmss")
                    .format(new Date());
            String path = timeStamp
                    + ".png";

            Log.i("image name : ", path);

            OutputStream stream = null;
            String sdCardDirectory = Environment.getExternalStorageDirectory() + "/"
                    + "judad";

            Log.i("image path : ", sdCardDirectory);
            if (pos == 1) {
                frontimage = new File(sdCardDirectory, path);
                if (frontimage.exists()) {
                    Log.i("info", "file exist checking 1 ");
                    frontimage.delete();
                    Log.i("info", "file exist delete");
                }
                try {
                    stream = new FileOutputStream(frontimage);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
            if (pos == 3) {
                frontimagesecond = new File(sdCardDirectory, path);
                if (frontimagesecond.exists()) {
                    Log.i("info", "file exist checking 1 ");
                    frontimagesecond.delete();
                    Log.i("info", "file exist delete");
                }
                try {
                    stream = new FileOutputStream(frontimagesecond);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
            if (pos == 5) {
                frontimagethird = new File(sdCardDirectory, path);
                if (frontimagethird.exists()) {
                    Log.i("info", "file exist checking 1 ");
                    frontimagethird.delete();
                    Log.i("info", "file exist delete");
                }
                try {
                    stream = new FileOutputStream(frontimagethird);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
            if (pos == 7) {
                frontimagefourth = new File(sdCardDirectory, path);
                if (frontimagefourth.exists()) {
                    Log.i("info", "file exist checking 1 ");
                    frontimagefourth.delete();
                    Log.i("info", "file exist delete");
                }
                try {
                    stream = new FileOutputStream(frontimagefourth);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }

//            if (frontimage.exists()) {
//                Log.i("info", "file exist checking 1 ");
//                frontimage.delete();
//                Log.i("info", "file exist delete");
//            }
//            try {
//                stream = new FileOutputStream(frontimage);
//            } catch (FileNotFoundException e) {
//                e.printStackTrace();
//            }
            retrievedBitmap.compress(Bitmap.CompressFormat.JPEG, 90,
                    stream);
            //retrievedBitmap.recycle();


            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //panCardImageFileName.panCardImageFileName(imagePanCard);
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void commonImageToFileGalery(Bitmap retrievedBitmap, String path, int pos) {
        try {
            String timeStamp = new SimpleDateFormat("yyyMMdd_HHmmss")
                    .format(new Date());
            String paths = timeStamp
                    + ".png";

            Log.i("image name : ", path);

            OutputStream stream = null;

            Log.i("image path : ", path);
            if (pos == 2) {
                frontimage = new File(path);
            }
            if (pos == 4) {
                frontimagesecond = new File(path);
            }
            if (pos == 6) {
                frontimagethird = new File(path);
            }
            if (pos == 8) {
                frontimagefourth = new File(path);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    enter code here

【问题讨论】:

  • 您能指出导致此崩溃的代码行吗?
  • 压缩后检索到Bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
  • 你的retrievedBitmap已经被回收了,但是我不知道为什么在你提供的代码中,请像调用commonImageToFile的方法一样发布更多代码

标签: android bitmap image-compression


【解决方案1】:

您将回收的位图传递给您的commonImageToFile 方法。您可以检查您的位图是否被回收:

if (retrievedBitmap.isRecycled()) { 
    // do your error handling here
    return; 
}

这必须在您致电retrievedBitmap.compress(...) 之前完成。 如果您捕获布局并想保存它,这个问题可能会对您有所帮助:Can't compress a recycled bitmap

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 2018-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多