save Bitmap with FileOutputStream - Android Graphics

Android examples for Graphics:Bitmap File

Description

save Bitmap with FileOutputStream

Demo Code


//package com.book2s;
import android.graphics.Bitmap;
import android.os.Environment;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    public static final String SDCARD_DIR = Environment
            .getExternalStorageDirectory().getAbsolutePath();
    private static final String FILE_DIR_NAME = "clipimage";

    public static boolean saveBitmap(Bitmap bm) {
        if (bm == null) {
            return false;
        }/*w  w w. ja v a  2  s .c o  m*/
        File fileDir = new File(SDCARD_DIR + File.separator + FILE_DIR_NAME);
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        File f = new File(fileDir.getAbsolutePath(), "result.png");
        if (f.exists()) {
            f.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
            return true;
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    }
}

Related Tutorials