store Image by Quality - Android Graphics

Android examples for Graphics:Bitmap Save

Description

store Image by Quality

Demo Code


//package com.java2s;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    @SuppressLint("LongLogTag")
    public static Uri storeImage(Bitmap image, int quality) {
        Uri uri = null;/*from   ww w  . j a  va2  s.c om*/
        try {
            String dcimPath = Environment
                    .getExternalStoragePublicDirectory(
                            Environment.DIRECTORY_DCIM).getAbsolutePath();
            String timeStamp = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss")
                    .format(new Date());
            String filePath = String.format("%s/DCIM%s.png", dcimPath,
                    timeStamp);
            File pictureFile = new File(filePath);
            if (pictureFile == null) {
                return null;
            }
            FileOutputStream fos = new FileOutputStream(pictureFile);
            image.compress(Bitmap.CompressFormat.PNG, quality, fos);
            fos.close();
            uri = new Uri.Builder().appendEncodedPath(filePath).build();
        } catch (Exception e) {
            Log.d("private void storeImage(Bitmap image)", e.toString());
        } finally {
            return uri;
        }
    }
}

Related Tutorials