save Image To Local - Android Graphics

Android examples for Graphics:Bitmap Save

Description

save Image To Local

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;

public class Main {

    public static void saveImgToLocal(String imagePath, Bitmap bm) {
        if (bm == null || imagePath == null || "".equals(imagePath)) {
            return;
        }//from ww w  .  j  a  v a2 s. c  o  m
        File f = new File(imagePath);
        if (f.exists()) {
            return;
        } else {
            try {
                File parentFile = f.getParentFile();
                if (!parentFile.exists()) {
                    parentFile.mkdirs();
                }
                f.createNewFile();
                FileOutputStream fos;
                fos = new FileOutputStream(f);
                bm.compress(Bitmap.CompressFormat.PNG, 100, fos);
                fos.close();
            } catch (FileNotFoundException e) {
                f.delete();
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
                f.delete();
            }
        }
    }
}

Related Tutorials