无法压缩回收的位图错误

Baw*_*tra 1 android bitmap

我的应用程序中出现“无法压缩回收的位图错误”。它发生picture.compress(Bitmap.CompressFormat.PNG, 90, fos);在我的代码中:

    private void savePicture(byte[] data, String gameId, String folderName ) {
        File pictureFile = getOutputMediaFile(gameId, folderName);
        if (pictureFile == null){
            Log.error("Error creating media file, check storage permissions.");
            return;
        }

        Bitmap picture = BitmapFactory.decodeByteArray(data, 0, data.length);

        picture = getResizedBitmap(picture, bitmapWidth, bitmapHeight);

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);

            picture.compress(Bitmap.CompressFormat.PNG, 90, fos);
            fos.close();
            picture.recycle();

            Log.info(">>> CameraActivity: PICTURE SAVED!");

        } catch (FileNotFoundException e) {
            Log.error(LibUtil.getStackTrace(e));
        } catch (IOException e) {
            Log.error(LibUtil.getStackTrace(e));
        }
    }

    public Bitmap getResizedBitmap(Bitmap bmp, int newWidth, int newHeight) {
        int width = bmp.getWidth();
        int height = bmp.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // CREATE A MATRIX FOR THE MANIPULATION
        Matrix matrix = new Matrix();
        // RESIZE THE BIT MAP
        matrix.postScale(scaleWidth, scaleHeight);

        // "RECREATE" THE NEW BITMAP
        Bitmap resizedBitmap = Bitmap.createBitmap(
                bmp, 0, 0, width, height, matrix, false);
        bmp.recycle();
        return resizedBitmap;
    }
Run Code Online (Sandbox Code Playgroud)

我发现的特别之处在于,当我注释该bmp.recycle();行时,错误消失了。看起来像(IMHO),resizedBitmap并且bmp正在引用相同的位图。但是我不知道这是正确的做法,是否存在内存泄漏或我的应用程序中没有其他内容。

顺便说一句,此代码不会在任何位置显示位图,ImageView而只是在场景后面定期从相机拍摄一张照片并将其保存。

提前致谢。

Bla*_*elt 5

createBitmap的版本并不总是返回新的Bitmap。在您的情况下,如果resizedBitmap等于bmp,则您都在回收这两个资源(相同的参考)。在您的getResizedBitmap添加中

 if (resizedBitamp != bmp) {
    bmp.recycle();
 }
Run Code Online (Sandbox Code Playgroud)

但是我不知道这是正确的做法,是否存在内存泄漏或我的应用程序中没有其他内容。

与内存泄漏无关。