save Bitmap to jpeg - Android android.graphics

Android examples for android.graphics:JPEG

Description

save Bitmap to jpeg

Demo Code

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.lemon.assist.Check;
import android.lemon.config.BaseConfig;
import android.os.Environment;
import android.os.StatFs;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.text.DecimalFormat;
import java.util.Comparator;

public class Main{
    public static String saveBitmap(Context context, Bitmap bm) {
        File f = new File(getImageDownloadDir(context),
                System.currentTimeMillis() + ".jpg");
        if (f.exists()) {
            f.delete();/*from w  w  w.  j a v  a  2s  .co m*/
        }
        try {
            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
            return f.getPath();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }

}

Related Tutorials