Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 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;
- }
- Bitmap bitmap= BitmapFactory.decodeResource(....);
- bitmap= bitmap.copy(Bitmap.Config.ARGB_8888, true);
- BitmapFactory.Options opt = new BitmapFactory.Options();
- opt.inMutable = true;
- Bitmap bp = BitmapFactory.decodeResource(getResources(), R.raw.white, opt);
- /**decodes a bitmap from a resource id. returns a mutable bitmap no matter what is the API level.<br/>
- might use the internal storage in some cases, creating temporary file that will be deleted as soon as it isn't finished*/
- @TargetApi(Build.VERSION_CODES.HONEYCOMB)
- public static Bitmap decodeMutableBitmapFromResourceId(final Context context, final int bitmapResId) {
- final Options bitmapOptions = new Options();
- if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB)
- bitmapOptions.inMutable = true;
- Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), bitmapResId, bitmapOptions);
- if (!bitmap.isMutable())
- bitmap = convertToMutable(context, bitmap);
- return bitmap;
- }
- @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
- public static Bitmap convertToMutable(final Context context, final Bitmap imgIn) {
- final int width = imgIn.getWidth(), height = imgIn.getHeight();
- final Config type = imgIn.getConfig();
- File outputFile = null;
- final File outputDir = context.getCacheDir();
- try {
- outputFile = File.createTempFile(Long.toString(System.currentTimeMillis()), null, outputDir);
- outputFile.deleteOnExit();
- final RandomAccessFile randomAccessFile = new RandomAccessFile(outputFile, "rw");
- final FileChannel channel = randomAccessFile.getChannel();
- final MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, imgIn.getRowBytes() * height);
- imgIn.copyPixelsToBuffer(map);
- imgIn.recycle();
- final Bitmap result = Bitmap.createBitmap(width, height, type);
- map.position(0);
- result.copyPixelsFromBuffer(map);
- channel.close();
- randomAccessFile.close();
- outputFile.delete();
- return result;
- } catch (final Exception e) {
- } finally {
- if (outputFile != null)
- outputFile.delete();
- }
- return null;
- }
- Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
- final JniBitmapHolder bitmapHolder=new JniBitmapHolder(bitmap);
- bitmap.recycle();
- bitmap=bitmapHolder.getBitmapAndFree();
- Log.d("DEBUG",""+bitmap.isMutable()); //will return true
Add Comment
Please, Sign In to add comment