保存图片文件的工具类

来源:互联网 发布:endnote 在线数据库 编辑:程序博客网 时间:2024/06/16 19:49

用法 先定义一个 全局变量

private SaveToSdCardUtil saveToSdCardUtil;

初始化工具类
在oncreate 中

saveToSdCardUtil = new SaveToSdCardUtil(context);

先得到 图片的资源 或者 通过url 下载图片得到Bitmap 图片

               一参 为 要保存的图片的名字               二参 为 保存的图片资源   没有  填0               三参 为 要保存的 Bitmap 图片  没有填 null

一参 必须 指定 , 二 , 三 参数 根据需求 自行设置
saveToSdCardUtil.saveImageFile(“xxx.jpg”, 0, bmp);

下面是工具类 直接 粘贴 至 工程中 即可 使用

import android.content.Context;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Environment;import android.util.Log;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;/** * Created by 保存图片工具类on 2016/7/29. */public class SaveToSdCardUtil {    private Context context;    //要存的文件绝对路径    String filePath;    //要存的文件绝对路径    private String fileCashPath;    public SaveToSdCardUtil(Context contexts) {        this.context = contexts;    }    //保存图片文件    public void saveImageFile(String fileName, int imageRes, Bitmap bitmap) {        saveToCash(fileName, imageRes, bitmap);        //要存的文件        File saveFile;        try {            if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())                    && Environment.getExternalStorageDirectory().exists()) {                filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + fileName;//  /storage/sdcard0                saveFile = new File(filePath);            } else {                filePath = context.getFilesDir().getAbsolutePath() + File.separator + fileName;                saveFile = new File(filePath);                String cmd = "chmod 777 " + saveFile.getAbsolutePath();                try {                    Runtime.getRuntime().exec(cmd);                } catch (IOException e) {                    e.printStackTrace();                }            }            //如果存在这个文件则先删除            if (saveFile.exists()) {                saveFile.delete();            }            // TODO  1                saveFile.createNewFile();            Bitmap pic = null;            if (bitmap != null) {                pic = bitmap;            } else {                pic = BitmapFactory.decodeResource(context.getResources(),                        imageRes);            }            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(saveFile)));            FileOutputStream fos = new FileOutputStream(saveFile);            pic.compress(Bitmap.CompressFormat.JPEG, 100, fos);            fos.flush();            fos.close();         //   java.io.IOException: open failed: EACCES (Permission denied)        } catch (Throwable t) {            t.printStackTrace();        }    }    private void saveToCash(String fileName, int imageRes, Bitmap bitmap) {        //要存的文件        File saveCashFile;        fileCashPath = context.getCacheDir().getAbsolutePath() + File.separator + fileName;        //创建文件        saveCashFile = new File(fileCashPath);        //如果存在这个文件则先删除        if (saveCashFile.exists()) {            saveCashFile.delete();        }        try {            saveCashFile.createNewFile();            Bitmap pic = null;            if (bitmap != null) {                pic = bitmap;            } else {                pic = BitmapFactory.decodeResource(context.getResources(),                        imageRes);            }            FileOutputStream fos = new FileOutputStream(saveCashFile);            pic.compress(Bitmap.CompressFormat.JPEG, 100, fos);            fos.flush();            fos.close();        } catch (IOException e) {            e.printStackTrace();        }    }    public String getFilePath() {        if (filePath != null && !filePath.equals("")) {            File testFile = new File(filePath);            if (!testFile.exists()) {                return fileCashPath;            } else {                Log.i("tag", "-filePath--" + filePath);                return filePath;            }        } else {            return fileCashPath;        }    }    //保存文件    public String getSaveFilePath(String fileName) {        //要存的文件        File saveFile = null;        try {            if (Environment.MEDIA_MOUNTED.equals(Environment                    .getExternalStorageState())                    && Environment.getExternalStorageDirectory().exists()) {                filePath = Environment.getExternalStorageDirectory()                        .getAbsolutePath() + File.separator + fileName;                //创建文件                saveFile = new File(filePath);            } else {                filePath = context.getFilesDir().getAbsolutePath() + File.separator                        + fileName;                //创建文件                saveFile = new File(filePath);                String cmd = "chmod 777 " + saveFile.getAbsolutePath();                try {                    Runtime.getRuntime().exec(cmd);                } catch (IOException e) {                    e.printStackTrace();                }            }            //如果存在这个文件则先删除            if (saveFile.exists()) {                saveFile.delete();            }            saveFile.createNewFile();        } catch (Throwable t) {            t.printStackTrace();        }        if (saveFile.getAbsolutePath().toString() != null && !saveFile.getAbsolutePath().toString().equals("")) {            return saveFile.getAbsolutePath().toString();        } else {            return "/sdcard/" + fileName;        }    }}
0 0
原创粉丝点击