save Bitmap - Android Graphics

Android examples for Graphics:Bitmap Save

Description

save Bitmap

Demo Code


//package com.java2s;

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

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

public class Main {

    public static boolean saveBitmap(Bitmap bitmap, String path) {

        try {//from www  . j  a  va 2s  .  co  m
            File file = new File(path);

            File parent = file.getParentFile();

            if (!parent.exists()) {
                parent.mkdirs();
            }

            FileOutputStream fos = new FileOutputStream(file);

            boolean b = bitmap
                    .compress(Bitmap.CompressFormat.PNG, 100, fos);

            fos.flush();
            fos.close();

            return b;

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;
    }

    public static boolean saveBitmap(Bitmap bitmap, String path,
            CompressFormat format, int quality) {

        try {
            File file = new File(path);
            File parent = file.getParentFile();

            if (!parent.exists()) {
                parent.mkdirs();
            }

            FileOutputStream fos = new FileOutputStream(file);

            boolean b = bitmap.compress(format, quality, fos);

            fos.flush();
            fos.close();

            return b;

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;
    }
}

Related Tutorials