save Bitmap Image to an auto generated File Path - Android Graphics

Android examples for Graphics:Bitmap Save

Description

save Bitmap Image to an auto generated File Path

Demo Code


import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Main{
    public static String saveImage(Bitmap bmp) {
        try {//from   ww  w.  j  a v a  2  s.  com
            File fo = CameraUtil.getOutputFile();
            FileOutputStream fos = new FileOutputStream(fo);
            try {
                bmp.compress(CompressFormat.JPEG, 90, fos);
                return fo.getAbsolutePath();
            } finally {
                fos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    static File getOutputFile() {
        File storage = new File(Environment.getExternalStorageDirectory(),
                "BasinMirror");
        storage.mkdirs();
        String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                Locale.getDefault()).format(new Date());
        return new File(storage, "IMG_" + timestamp + ".jpg");
    }
}

Related Tutorials