Android How to - Save Bitmap to file path








Question

We would like to know how to save Bitmap to file path.

Answer

//from   w  ww  .  j  a v a2 s  . c  om
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;

public class Main {
  public static void saveBitmapToFileCache(Bitmap bitmap, String strFilePath) {
    File copyFile = new File(strFilePath);

    OutputStream out = null;

    try {

      copyFile.createNewFile();
      out = new FileOutputStream(copyFile);

      if (bitmap.compress(CompressFormat.PNG, 70, out))
        ;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        out.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}