get Mutable For Bitmap - Android Graphics

Android examples for Graphics:Bitmap Create

Description

get Mutable For Bitmap

Demo Code


//package com.java2s;
import java.io.File;
import java.io.FileNotFoundException;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;

import android.os.Environment;

public class Main {
    public static Bitmap getMutableFor(Bitmap original) {
        Bitmap mutable = null;// w w w.ja  v a2 s.c o  m
        try {
            File file = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "temp.tmp");
            RandomAccessFile randomAccessFile = new RandomAccessFile(file,
                    "rw");
            int width = original.getWidth();
            int height = original.getHeight();
            Config type = original.getConfig();

            FileChannel channel = randomAccessFile.getChannel();
            MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0,
                    original.getRowBytes() * height);
            original.copyPixelsToBuffer(map);
            original.recycle();
            System.gc();// try to force the bytes from the imgIn to be released
            original = null;
            mutable = Bitmap.createBitmap(width, height, type);
            map.position(0);
            mutable.copyPixelsFromBuffer(map);
            channel.close();
            randomAccessFile.close();
            file.delete();

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

        return mutable;
    }
}

Related Tutorials