保存图片文件的工具类

用法 先定义一个 全局变量

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值