只有Android 4.1,Bitmap会自动回收

use*_*071 5 android bitmap recycle drawable android-4.2-jelly-bean

我有非常严重的问题.只有Android 4.1,Bitmap会自动回收!我没有在我的代码中调用recycle()!我的项目在其他操作系统版本(~4.0.3)中可以正常使用任何分辨率.其他项目也有同样的问题.

所有图像文件都在drawable-nodpi文件夹中.我总是调整它们以适应任何设备的分辨率.

public Bitmap GetBitmap(int resource){

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inDither = true;
    options.inPurgeable = true;

    Bitmap tmp = null;

    try{
        tmp = BitmapFactory.decodeResource(mResources, resource, options);
    }catch(OutOfMemoryError e){
        options.inSampleSize = 2;
        tmp = BitmapFactory.decodeResource(mResources, resource, options);
    }

    return tmp;
}

public Bitmap GetScaledBitmap(int resource, int width, int height, boolean filter){

    Bitmap tmp = GetBitmap(resource);

    Bitmap img = Bitmap.createScaledBitmap(tmp, width, height, filter);

    tmp.recycle();
    tmp = null;

    return img;
}
Run Code Online (Sandbox Code Playgroud)

在我的测试中,

  • 相同的位图实例,但出现问题取决于调整大小值.

ex)int width = 100 ;

位图imgStar = MyResourceManager.getInstance().GetScaledBitmap(R.drawable.star,width,width,true); - >返回循环实例.

width = 200 ;

imgStar = MyResourceManager.getInstance().GetScaledBitmap(R.drawable.star,width,width,true); - >返回普通实例.

  • 在不同的分辨率下,imgStar工作正常,但问题出现在其他位图实例中.同样,当我更改调整大小值时,它工作正常.

  • 在相同的分辨率,如果我更改图像文件文件夹的名称,问题发生在其他位图实例中.drawable-nodpi - > drawable - > drawable-ldpi,...,drawable-xdpi.

  • 相同的调整大小值,如果我把其他资源ID,它工作正常.EX)

int width = 100;

位图imgStar = MyResourceManager.getInstance()GetScaledBitmap(R.drawable.,宽度,宽度,TRUE); - >返回循环实例.

imgStar = MyResourceManager.getInstance()GetScaledBitmap(R.drawable.钻石,宽度,宽度,TRUE); - >返回普通实例.

请...我该怎么办?!T ^ T.

小智 6

您从不同大小获得不同结果的原因可能是因为createScaledBitmap将返回原始对象(如果它与您缩放的大小相同).

我有同样的问题,做同样的事情.我能够通过这种方式修复它:

public Bitmap GetScaledBitmap(int resource, int width, int height, boolean filter) {

    Bitmap tmp = GetBitmap(resource);
    Bitmap img = Bitmap.createScaledBitmap(tmp, width, height, filter);
    //copy the image to be sure you are not using the same object as the tmp bitmap
    img=img.copy (Bitmap.Config.RGB_565,false);
    tmp.recycle();
    tmp = null;
    return img;
}
Run Code Online (Sandbox Code Playgroud)

在这里我复制了位图,以确保在我回收tmp位图之前它不仅仅是对tmp位图对象的引用.当然,您可以使用您需要的任何位图配置.