android - 回收位图异常

标签 android

我遇到了这个异常:

异常:java.lang.IllegalStateException:无法复制回收位图

我的代码是:

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int newWidth;
    int newHeight;
    if (width >= height) {
        newWidth = Math.min(width,1024);
        newHeight = (int) (((float)newWidth)*height/width);
    }
    else {
        newHeight = Math.min(height, 1024);
        newWidth = (int) (((float)newHeight)*width/height);
    }
    float scaleWidth = ((float)newWidth)/width;
    float scaleHeight = ((float)newHeight)/height;

    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    switch (orientation) {
    case 3:
        matrix.postRotate(180);
        break;
    case 6:
        matrix.postRotate(90);
        break;
    case 8:
        matrix.postRotate(270);
        break;
    }
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    bitmap.recycle();
    try {
        bitmap = resizedBitmap.copy(resizedBitmap.getConfig(), true);
    }
    catch (Exception e) {
        Log.v(TAG,"Exception: "+e);
    }

如果异常告诉我我已经回收了 resizedBitmap,那显然是错误的!我做错了什么??

最佳答案

您实际上是在这一行之后调用 bitmap.recycle();:

Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

关于android - 回收位图异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9071624/

相关文章:

java - 查看 Binding with Java 11,Android Studio 总是显示错误(但运行没有任何问题)

安卓刷新 Activity

android - 回合制游戏玩家无需对手调用 takeTurn 即可接收自己的数据

android - TextWatcher 的 onTextChanged、beforeTextChanged 和 afterTextChanged 的​​区别

android - 初始初始化组织

android - 在固定时间间隔后调用特定方法

java - 为什么我的 Android PieChart 绘图行为异常并且在屏幕旋转时出现重复的切片?

android - 下拉菜单中的 Ancher 字体大小在移动浏览器中发生变化

java - 适用于 Android/Java 的正确代码设计

android - 计算 sqlite,android 中特定列值的出现次数?