Android Bitmap Compress bitmap2File(Bitmap bitmap, File file, CompressFormat format, int quality)

Here you can find the source of bitmap2File(Bitmap bitmap, File file, CompressFormat format, int quality)

Description

bitmap File

Declaration

public static final void bitmap2File(Bitmap bitmap, File file,
            CompressFormat format, int quality) 

Method Source Code

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ExifInterface;
import android.media.ThumbnailUtils;
import android.util.Base64;
import android.view.View;

public class Main{
    public static final void bitmap2File(Bitmap bitmap, File file) {
        bitmap2File(bitmap, file, CompressFormat.JPEG, 100);
    }//from   w  ww  . jav a  2 s.c  o  m
    public static final void bitmap2File(Bitmap bitmap, File file,
            CompressFormat format, int quality) {
        if (bitmap == null) {
            throw new NullPointerException("source bitmap is null...");
        }
        if (file == null) {
            throw new NullPointerException("targe file is null...");
        }
        if (file.exists()) {
            file.delete();
        }
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            LogUtility.d(e);
        }
        if (outputStream == null) {
            return;
        }
        bitmap.compress(format, quality, outputStream);
        try {
            outputStream.flush();
        } catch (IOException e) {
            LogUtility.d(e);
        } finally {
            StreamUtility.closeStream(outputStream);
        }
    }
}

Related

  1. bitmap2Stream(Bitmap bitmap, CompressFormat format, int quality)
  2. compressBitmapToFile(Bitmap originalBitmap, String path, int quality)
  3. compressToBytes(Bitmap bitmap)
  4. compressToBytes(Bitmap bitmap, int quality)