SHARE
TWEET

BitmapFactory.decodeResource returns a mutable Bitmap in Android 2.2 and an immutable Bitmap in Android 1.6

a guest Mar 19th, 2012 1,037 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Converts a immutable bitmap to a mutable bitmap. This operation doesn't allocates
  3.  * more memory that there is already allocated.
  4.  *
  5.  * @param imgIn - Source image. It will be released, and should not be used more
  6.  * @return a copy of imgIn, but imuttable.
  7.  */
  8. public static Bitmap convertToMutable(Bitmap imgIn) {
  9.     try {
  10.         //this is the file going to use temporally to save the bytes.
  11.         // This file will not be a image, it will store the raw image data.
  12.         File file = new File(Environment.getExternalStorageDirectory() + File.separator + "temp.tmp");
  13.  
  14.         //Open an RandomAccessFile
  15.         //Make sure you have added uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
  16.         //into AndroidManifest.xml file
  17.         RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
  18.  
  19.         // get the width and height of the source bitmap.
  20.         int width = imgIn.getWidth();
  21.         int height = imgIn.getHeight();
  22.         Config type = imgIn.getConfig();
  23.  
  24.         //Copy the byte to the file
  25.         //Assume source bitmap loaded using options.inPreferredConfig = Config.ARGB_8888;
  26.         FileChannel channel = randomAccessFile.getChannel();
  27.         MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, imgIn.getRowBytes()*height);
  28.         imgIn.copyPixelsToBuffer(map);
  29.         //recycle the source bitmap, this will be no longer used.
  30.         imgIn.recycle();
  31.         System.gc();// try to force the bytes from the imgIn to be released
  32.  
  33.         //Create a new bitmap to load the bitmap again. Probably the memory will be available.
  34.         imgIn = Bitmap.createBitmap(width, height, type);
  35.         map.position(0);
  36.         //load it back from temporary
  37.         imgIn.copyPixelsFromBuffer(map);
  38.         //close the temporary file and channel , then delete that also
  39.         channel.close();
  40.         randomAccessFile.close();
  41.  
  42.         // delete the temp file
  43.         file.delete();
  44.  
  45.     } catch (FileNotFoundException e) {
  46.         e.printStackTrace();
  47.     } catch (IOException e) {
  48.         e.printStackTrace();
  49.     }
  50.  
  51.     return imgIn;
  52. }
  53.        
  54. Bitmap immutableBitmap = BitmapFactory.decodeResource(....);
  55. Bitmap mutableBitmap = immutableBitmap.copy(Bitmap.Config.ARGB_8888, true);
  56.        
  57. BitmapFactory.Options opt = new BitmapFactory.Options();
  58.  opt.inMutable = true;
  59.  Bitmap bp = BitmapFactory.decodeResource(getResources(), R.raw.white, opt);
RAW Paste Data
Top