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

booleansaveBitmapToFile(Bitmap bitmap, String filename)
save Bitmap To File
boolean result = false;
FileOutputStream fos = null;
try {
    fos = new FileOutputStream(new File(filename));
    fos.write(bitmap2byte(bitmap));
    result = true;
} catch (FileNotFoundException e) {
    e.printStackTrace();
...
booleansaveBitmap(Bitmap bmp, String path, CompressFormat format)
save Bitmap
if (bmp == null || path == null) {
    return false;
try {
    File file = new File(path);
    if (!file.exists()) {
        file.createNewFile();
    FileOutputStream fos = new FileOutputStream(file);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(format, 100, baos);
    fos.write(baos.toByteArray());
    baos.flush();
    fos.flush();
    baos.close();
    fos.close();
    return true;
} catch (Exception e) {
    e.printStackTrace();
    return false;
StringsaveBitmap(Bitmap image)
save Bitmap
String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm")
        .format(new Date());
String mImageName = "IP_" + timeStamp + ".jpg";
try {
    FileOutputStream fos = cntxt.openFileOutput(mImageName,
            Context.MODE_PRIVATE);
    image.compress(Bitmap.CompressFormat.PNG, 90, fos);
    fos.close();
...
voidsaveBitmap(File file, Bitmap bitmap)
save Bitmap
FileOutputStream fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
booleansaveBitmap(File saveToFile, Bitmap bitmapToSave)
save Bitmap
try {
    FileOutputStream fileOutputStream = new FileOutputStream(
            saveToFile);
    bitmapToSave.compress(Bitmap.CompressFormat.JPEG, 100,
            fileOutputStream);
    fileOutputStream.flush();
    fileOutputStream.close();
} catch (FileNotFoundException fileNotFoundException) {
...
voidsaveBitmapAsJpgAtSd(Bitmap mBitmap, String dirPath, String fileName)
save Bitmap As Jpg At Sd
String fullPath = Environment.getExternalStorageDirectory()
        .toString() + "/" + dirPath + "/" + fileName;
FileOutputStream fos = null;
File file = new File(fullPath);
file.getParentFile().mkdirs();
fos = new FileOutputStream(file);
mBitmap.compress(CompressFormat.JPEG, 100, fos);
fos.close();
...
voidsaveBitmapFromView(View view, String path)
save Bitmap From View
if (view != null) {
    Bitmap bitmap = view.getDrawingCache();
    try {
        FileOutputStream fos = new FileOutputStream(path);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
    } catch (Exception e) {
        e.printStackTrace();
booleansaveBitmapJPEGWithBackgroundColor( String strFileName, Bitmap bitmap, int nQuality, int nBackgroundColor)
Save jpeg image with background color
boolean bSuccess1 = false;
boolean bSuccess2 = false;
boolean bSuccess3;
File saveFile = new File(strFileName);
if (saveFile.exists()) {
    if (!saveFile.delete())
        return false;
int nA = (nBackgroundColor >> 24) & 0xff;
if (nA == 0)
    nBackgroundColor = 0xFFFFFFFF;
Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(),
        bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
canvas.drawColor(nBackgroundColor);
canvas.drawBitmap(bitmap, rect, rect, new Paint());
if (nQuality < 10)
    nQuality = 10;
else if (nQuality > 100)
    nQuality = 100;
OutputStream out = null;
try {
    bSuccess1 = saveFile.createNewFile();
} catch (IOException e1) {
    e1.printStackTrace();
try {
    out = new FileOutputStream(saveFile);
    bSuccess2 = newBitmap.compress(CompressFormat.JPEG, nQuality,
            out);
} catch (Exception e) {
    e.printStackTrace();
try {
    if (out != null) {
        out.flush();
        out.close();
        bSuccess3 = true;
    } else
        bSuccess3 = false;
} catch (IOException e) {
    e.printStackTrace();
    bSuccess3 = false;
} finally {
    if (out != null) {
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
return (bSuccess1 && bSuccess2 && bSuccess3);
booleansaveBitmapPNGWithBackgroundColor( String strFileName, Bitmap bitmap, int nBackgroundColor)
Save PNG image with background color
boolean bSuccess1 = false;
boolean bSuccess2 = false;
boolean bSuccess3;
File saveFile = new File(strFileName);
if (saveFile.exists()) {
    if (!saveFile.delete())
        return false;
int nA = (nBackgroundColor >> 24) & 0xff;
if (nA == 0)
    nBackgroundColor = 0xFFFFFFFF;
Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(),
        bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
canvas.drawColor(nBackgroundColor);
canvas.drawBitmap(bitmap, rect, rect, new Paint());
OutputStream out = null;
try {
    bSuccess1 = saveFile.createNewFile();
} catch (IOException e1) {
    e1.printStackTrace();
try {
    out = new FileOutputStream(saveFile);
    bSuccess2 = newBitmap.compress(CompressFormat.PNG, 100, out);
} catch (Exception e) {
    e.printStackTrace();
try {
    if (out != null) {
        out.flush();
        out.close();
        bSuccess3 = true;
    } else
        bSuccess3 = false;
} catch (IOException e) {
    e.printStackTrace();
    bSuccess3 = false;
} finally {
    if (out != null) {
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
return (bSuccess1 && bSuccess2 && bSuccess3);
voidsaveBitmapToPath(Context ctxt, String path, String filename, Bitmap bm)
save Bitmap To Path
File directory = new File(
        Environment.getExternalStorageDirectory(), path);
if (!directory.exists())
    directory.mkdir();
File lastFile = new File(directory, filename);
try {
    if (lastFile.createNewFile() == false) {
        lastFile.delete();
...