Transformation scale mutated input Bitmap但是没能回收原来的

Sni*_*pet 2 java android

我有以下代码可以根据屏幕的大小缩放图像,这样它就不会被破坏。我的问题是当我把这个函数进行转换时,我总是得到这个错误

java.lang.IllegalStateException: 变换比例改变了输入位图但未能回收原始。

  @SuppressWarnings("deprecation")
      @Override
      public Bitmap transform(Bitmap bitmap) {

        if (bitmap != null) {


          if (bitmap.getWidth() < MAX_WIDTH) { 
            bitmap = this.scaledDownBitmap(bitmap);
          }


          boolean flag = true;


          int deviceWidth =  ((Activity)this.context).getWindowManager().getDefaultDisplay().getWidth();
          int deviceHeight = ((Activity)this.context).getWindowManager().getDefaultDisplay().getHeight();

          int bitmapHeight = bitmap.getHeight(); // 563
          int bitmapWidth = bitmap.getWidth(); // 900

          // aSCPECT rATIO IS Always WIDTH x HEIGHT rEMEMMBER 1024 x 768

          if (bitmapWidth > deviceWidth) {
            flag = false;

            // scale According to WIDTH
            int scaledWidth = deviceWidth;
            int scaledHeight = (scaledWidth * bitmapHeight) / bitmapWidth;

            try {
              if (scaledHeight > deviceHeight)
                scaledHeight = deviceHeight;

              bitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);
            } catch (Exception e) {
              e.printStackTrace();
            }
          }

          if (flag) {
            if (bitmapHeight > deviceHeight) {
              // scale According to HEIGHT
              int scaledHeight = deviceHeight;
              int scaledWidth = (scaledHeight * bitmapWidth) / bitmapHeight;

              try {
                if (scaledWidth > deviceWidth)
                  scaledWidth = deviceWidth;

                bitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);
              } catch (Exception e) {
                e.printStackTrace();
              }
            }
          }
        }
        return bitmap;

      }
Run Code Online (Sandbox Code Playgroud)

Rod*_*uin 5

您需要创建一个局部变量Bitmap,每次缩放位图时都会引用它,然后调用原始位图的recycle方法以释放其内存。

例子:

public Bitmap transform(Bitmap bitmap) {

    Bitmap bitmap2;
    Bitmap bitmap3;

    if (bitmap != null) {


      if (bitmap.getWidth() < MAX_WIDTH) { 
        bitmap = this.scaledDownBitmap(bitmap);
      }


      boolean flag = true;


      int deviceWidth =  ((Activity)this.context).getWindowManager().getDefaultDisplay().getWidth();
      int deviceHeight = ((Activity)this.context).getWindowManager().getDefaultDisplay().getHeight();

      int bitmapHeight = bitmap.getHeight(); // 563
      int bitmapWidth = bitmap.getWidth(); // 900

      // aSCPECT rATIO IS Always WIDTH x HEIGHT rEMEMMBER 1024 x 768

      if (bitmapWidth > deviceWidth) {
        flag = false;

        // scale According to WIDTH
        int scaledWidth = deviceWidth;
        int scaledHeight = (scaledWidth * bitmapHeight) / bitmapWidth;

        try {
          if (scaledHeight > deviceHeight)
            scaledHeight = deviceHeight;

          bitmap2 = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true); //reference the scaled bitmap to local bitmap
          bitmap.recycle(); //recycle the original bitmap

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

      if (flag) {
        if (bitmapHeight > deviceHeight) {
          // scale According to HEIGHT
          int scaledHeight = deviceHeight;
          int scaledWidth = (scaledHeight * bitmapWidth) / bitmapHeight;

          try {
            if (scaledWidth > deviceWidth)
              scaledWidth = deviceWidth;

            bitmap3 = Bitmap.createScaledBitmap(bitmap2, scaledWidth, scaledHeight, true);
            bitmap2.recycle();
            return bitmap3;

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

  }
Run Code Online (Sandbox Code Playgroud)