save Image to a path by type - Android Graphics

Android examples for Graphics:Bitmap File

Description

save Image to a path by type

Demo Code


//package com.book2s;
import java.io.File;

import java.io.FileOutputStream;

import android.graphics.Bitmap;

import android.os.Environment;

public class Main {

    public static String saveImg(Bitmap b, String name, String type)
            throws Exception {
        String path = Environment.getExternalStorageDirectory().getPath()
                + File.separator + "HaoXin/zipImg/";
        File mediaFile = null;//from  w ww  .j  a  v a 2  s. c om
        if (!"".equals(type)) {
            mediaFile = new File(path + File.separator + name + "." + type);
        } else {
            mediaFile = new File(path + File.separator + name + ".jpg");
        }
        if (mediaFile.exists()) {
            mediaFile.delete();
        }
        if (!new File(path).exists()) {
            new File(path).mkdirs();
        }
        mediaFile.createNewFile();
        FileOutputStream fos = new FileOutputStream(mediaFile);
        b.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
        b.recycle();
        b = null;
        System.gc();
        return mediaFile.getPath();
    }
}

Related Tutorials