Android Utililty Methods Bitmap Save

List of utility methods to do Bitmap Save

Description

The list of methods to do Bitmap Save are organized into topic(s).

Method

StringsaveImage(String filename, Bitmap bm)
Save the image as file
return BitmapUtil.saveImage(filename, bm, false);
StringsaveImage(String filename, Bitmap bm, boolean landscape)
Save the image as file
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()) {
    if (landscape) {
        Matrix matrix = new Matrix();
        matrix.postRotate(-90);
        bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
                bm.getHeight(), matrix, true);
    try {
        File directory = new File(root.getPath() + ROOT_DIRECTORY);
        if (!directory.exists()) {
            directory.mkdir();
        String filepath = directory.getPath() + "/" + filename;
        File file = new File(filepath);
        FileOutputStream fos = new FileOutputStream(file);
        bm.compress(Bitmap.CompressFormat.PNG, 0, fos);
        fos.close();
        return filepath;
    } catch (Exception e) {
        Log.e("ARTags:BitmapUtil:saveImage",
                "Exception while writing the tag", e);
} else {
    Log.e("ARTags:BitmapUtil:saveImage",
            "Can't write on the volume");
return null;
voidsaveMyBitmap(String bitName, Bitmap mBitmap)
save My Bitmap
File f = new File(bitName);
try {
    f.createNewFile();
} catch (IOException e1) {
    e1.printStackTrace();
FileOutputStream fOut = null;
try {
...
StringsaveToLocal(Bitmap bmp, String fileName)
Save to local.
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory()
        + File.separator + fileName);
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
...
voidstoreImage(Context context, Bitmap image)
store Image
File pictureFile = getOutputMediaFile(context);
if (pictureFile == null)
    return;
try {
    FileOutputStream fos = new FileOutputStream(pictureFile);
    image.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    fos.close();
} catch (Exception e) {
...
booleansaveBitmap(Bitmap bitmap, String filename)
save Bitmap
if (bitmap == null)
    return false;
try {
    OutputStream out = new FileOutputStream(filename);
    ;
    bitmap.compress(CompressFormat.JPEG, 100, out);
    out.close();
    return true;
...
booleansaveBitmap2SD(Bitmap bitmap, Context context, String filePath, String fileName)
save Bitmap SD
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { 
    return false;
String name = new SimpleDateFormat().format(new Date()) + ".jpg";
String filePath1 = "/sdcard/DCIM/Camera/";
File file = new File(filePath1);
if (!file.exists()) {
...
UriaddImageAsApplication(ContentResolver cr, String name, long dateTaken, String directory, String filename, Bitmap source, byte[] jpegData)
add Image As Application
OutputStream outputStream = null;
String filePath = directory + "/" + filename;
try {
    File dir = new File(directory);
    if (!dir.exists()) {
        dir.mkdirs();
        Log.d(TAG, dir.toString() + " create");
    File file = new File(directory, filename);
    if (file.createNewFile()) {
        outputStream = new FileOutputStream(file);
        if (source != null) {
            source.compress(CompressFormat.JPEG, 75, outputStream);
        } else {
            outputStream.write(jpegData);
} catch (FileNotFoundException ex) {
    Log.w(TAG, ex);
    return null;
} catch (IOException ex) {
    Log.w(TAG, ex);
    return null;
} finally {
    if (outputStream != null) {
        try {
            outputStream.close();
        } catch (Throwable t) {
ContentValues values = new ContentValues(7);
values.put(Images.Media.TITLE, name);
values.put(Images.Media.DISPLAY_NAME, filename);
values.put(Images.Media.DATE_TAKEN, dateTaken);
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(Images.Media.DATA, filePath);
return cr.insert(IMAGE_URI, values);
voidsaveBitmapToFile(Bitmap bitmap, String path)
save Bitmap To File
CompressFormat format = Bitmap.CompressFormat.PNG;
int quality = 100;
OutputStream stream = null;
try {
    File file = new File(path);
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();
    stream = new FileOutputStream(path);
    bitmap.compress(format, quality, stream);
} catch (FileNotFoundException e) {
    e.printStackTrace();
} finally {
    if (stream != null) {
        try {
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
Stringsave(Bitmap bitmap, String fileName)
save
File folder = new File(Environment.getExternalStorageDirectory(),
        SDCARD_FOLDER);
if (!folder.exists() && !folder.mkdirs()) {
    folder = new File(Environment.getDataDirectory(), SDCARD_FOLDER);
File barcodeFile = new File(folder, fileName + ".png");
barcodeFile.delete();
FileOutputStream fos = null;
...