save Bitmap to file - Android Graphics

Android examples for Graphics:Bitmap File

Description

save Bitmap to file

Demo Code


//package com.java2s;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import java.io.OutputStream;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;

import android.os.Environment;

import android.util.Log;

public class Main {
    public static boolean saveBitmap2file(Bitmap bmp, String filename) {
        CompressFormat format = Bitmap.CompressFormat.JPEG;
        int quality = 100;
        OutputStream stream = null;
        try {//  w  w w  . j a v a 2s.  c  o  m
            stream = new FileOutputStream("/sdcard/" + filename);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return bmp.compress(format, quality, stream);
    }

    public static boolean saveBitmap2file(Bitmap bmp) {

        String FILE_PATH = "";
        CompressFormat format = Bitmap.CompressFormat.PNG;
        int quality = 100;
        OutputStream stream = null;
        try {
            // SDcard?
            if (!Environment.MEDIA_MOUNTED.equals(Environment
                    .getExternalStorageState())) {
                // 
                return false;
            }

            // SDcardT
            File SDCardRoot = Environment.getExternalStorageDirectory();
            if (SDCardRoot.getFreeSpace() < 10000) {
                //    ?
                Log.e("Utils", "?");
                return false;
            }

            // ?SDcard
            File bitmapFile = new File(SDCardRoot.getPath() + FILE_PATH);
            bitmapFile.getParentFile().mkdirs();// ?
            stream = new FileOutputStream(SDCardRoot.getPath() + FILE_PATH);// "/sdcard/"
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return bmp.compress(format, quality, stream);
    }
}

Related Tutorials