java.lang.IllegalStateException:无法压缩回收的位图

问题描述 投票:0回答:1

捕获图像并从图库中获取图像时出现此错误。

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
android bitmap image-compression
1个回答
0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.