BitmapFactory.decodeResource 在 Android 2.2 中返回可变位图,在 Android 1.6 中返回不可变位图 [英] BitmapFactory.decodeResource returns a mutable Bitmap in Android 2.2 and an immutable Bitmap in Android 1.6

查看:20
本文介绍了BitmapFactory.decodeResource 在 Android 2.2 中返回可变位图,在 Android 1.6 中返回不可变位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序并在运行 Android 2.2 的设备上测试它.在我的代码中,我使用了我使用 BitmapFactory.decodeResource 检索的位图,并且我可以通过调用 bitmap.setPixels() 对其进行更改.当我在朋友的运行 Android 1.6 的设备上测试时,我在对 bitmap.setPixels 的调用中得到一个 IllegalStateException.在线文档说,当位图不可变时,此方法会抛出 IllegalStateException.该文档没有说明 decodeResource 返回不可变位图的任何内容,但显然必须如此.

I am developing an application and testing it on my device running Android 2.2. In my code, I make use of a Bitmap that I retrieve using BitmapFactory.decodeResource, and I am able to make changes by calling bitmap.setPixels() on it. When I test this on a friend's device running Android 1.6, I get an IllegalStateException in the call to bitmap.setPixels. Documentation online says an IllegalStateException is thrown from this method when the bitmap is immutable. The documentation doesn't say anything about decodeResource returning an immutable bitmap, but clearly that must be the case.

是否有不同的调用可以从应用程序资源可靠地获取可变位图,而无需第二个 Bitmap 对象(我可以创建一个相同大小的可变位图并绘制到 Canvas 包装中它,但那将需要两个相同大小的位图,使用的内存是我预期的两倍)?

Is there a different call I can make to get a mutable bitmap reliably from an application resource without needing a second Bitmap object (I could create a mutable one the same size and draw into a Canvas wrapping it, but that would require two bitmaps of equal size using up twice as much memory as I had intended)?

推荐答案

您可以将不可变位图转换为可变位图.

You can convert your immutable bitmap to a mutable bitmap.

我找到了一个可以接受的解决方案,它只使用一个位图的内存.

I found an acceptable solution that uses only the memory of one bitmap.

一个源位图被原始保存(RandomAccessFile)在磁盘上(没有ram内存),然后源位图被释放,(现在,内存中没有位图),之后,文件信息被加载到另一个位图.这种方式可以制作一个位图副本,每次只在 ram 内存中存储一​​个位图.

A source bitmap is raw saved (RandomAccessFile) on disk (no ram memory), then source bitmap is released, (now, there's no bitmap at memory), and after that, the file info is loaded to another bitmap. This way is possible to make a bitmap copy having just one bitmap stored in ram memory per time.

在此处查看完整的解决方案和实现:Android:将不可变位图转换为可变位图

See the full solution and implementation here: Android: convert Immutable Bitmap into Mutable

我对此解决方案进行了改进,现在可用于任何类型的位图(ARGB_8888、RGB_565 等),并删除临时文件.看我的方法:

I add a improvement to this solution, that now works with any type of Bitmaps (ARGB_8888, RGB_565, etc), and deletes the temp file. See my method:

/**
 * Converts a immutable bitmap to a mutable bitmap. This operation doesn't allocates
 * more memory that there is already allocated.
 * 
 * @param imgIn - Source image. It will be released, and should not be used more
 * @return a copy of imgIn, but muttable.
 */
public static Bitmap convertToMutable(Bitmap imgIn) {
    try {
        //this is the file going to use temporally to save the bytes. 
        // This file will not be a image, it will store the raw image data.
        File file = new File(Environment.getExternalStorageDirectory() + File.separator + "temp.tmp");

        //Open an RandomAccessFile
        //Make sure you have added uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        //into AndroidManifest.xml file
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");

        // get the width and height of the source bitmap.
        int width = imgIn.getWidth();
        int height = imgIn.getHeight();
        Config type = imgIn.getConfig();

        //Copy the byte to the file
        //Assume source bitmap loaded using options.inPreferredConfig = Config.ARGB_8888;
        FileChannel channel = randomAccessFile.getChannel();
        MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, imgIn.getRowBytes()*height);
        imgIn.copyPixelsToBuffer(map);
        //recycle the source bitmap, this will be no longer used.
        imgIn.recycle();
        System.gc();// try to force the bytes from the imgIn to be released

        //Create a new bitmap to load the bitmap again. Probably the memory will be available. 
        imgIn = Bitmap.createBitmap(width, height, type);
        map.position(0);
        //load it back from temporary 
        imgIn.copyPixelsFromBuffer(map);
        //close the temporary file and channel , then delete that also
        channel.close();
        randomAccessFile.close();

        // delete the temp file
        file.delete();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } 

    return imgIn;
}

这篇关于BitmapFactory.decodeResource 在 Android 2.2 中返回可变位图,在 Android 1.6 中返回不可变位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆