Guest User

Untitled

a guest
Jul 22nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  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 muttable.
  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 bitmap= BitmapFactory.decodeResource(....);
  55. bitmap= bitmap.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);
  60.  
  61. /**decodes a bitmap from a resource id. returns a mutable bitmap no matter what is the API level.<br/>
  62. might use the internal storage in some cases, creating temporary file that will be deleted as soon as it isn't finished*/
  63. @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  64. public static Bitmap decodeMutableBitmapFromResourceId(final Context context, final int bitmapResId) {
  65. final Options bitmapOptions = new Options();
  66. if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB)
  67. bitmapOptions.inMutable = true;
  68. Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), bitmapResId, bitmapOptions);
  69. if (!bitmap.isMutable())
  70. bitmap = convertToMutable(context, bitmap);
  71. return bitmap;
  72. }
  73.  
  74. @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
  75. public static Bitmap convertToMutable(final Context context, final Bitmap imgIn) {
  76. final int width = imgIn.getWidth(), height = imgIn.getHeight();
  77. final Config type = imgIn.getConfig();
  78. File outputFile = null;
  79. final File outputDir = context.getCacheDir();
  80. try {
  81. outputFile = File.createTempFile(Long.toString(System.currentTimeMillis()), null, outputDir);
  82. outputFile.deleteOnExit();
  83. final RandomAccessFile randomAccessFile = new RandomAccessFile(outputFile, "rw");
  84. final FileChannel channel = randomAccessFile.getChannel();
  85. final MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, imgIn.getRowBytes() * height);
  86. imgIn.copyPixelsToBuffer(map);
  87. imgIn.recycle();
  88. final Bitmap result = Bitmap.createBitmap(width, height, type);
  89. map.position(0);
  90. result.copyPixelsFromBuffer(map);
  91. channel.close();
  92. randomAccessFile.close();
  93. outputFile.delete();
  94. return result;
  95. } catch (final Exception e) {
  96. } finally {
  97. if (outputFile != null)
  98. outputFile.delete();
  99. }
  100. return null;
  101. }
  102.  
  103. Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
  104. final JniBitmapHolder bitmapHolder=new JniBitmapHolder(bitmap);
  105. bitmap.recycle();
  106. bitmap=bitmapHolder.getBitmapAndFree();
  107. Log.d("DEBUG",""+bitmap.isMutable()); //will return true
Add Comment
Please, Sign In to add comment