Android 本地文件管理类

Android本地文件管理类:

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

public class FileUtil {

    private static final String TAG = "FileUtil";
    private Context context;
    private String SRCPATH;
    private String THUMBPATH;
    private boolean hasSD = false;

    /**
     * 单例模式
     */
    public FileUtil(Context context) {
        this.context = context;
        // 判断是否含有SD卡
        hasSD = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        String rootPath = hasSD ? (Environment.getExternalStorageDirectory().getPath() + File.separator + "Android" + File.separator + "data" + File.separator + SwitchAppTool.APPLICATION_ID + File.separator + "cache" + File.separator) : (this.context.getFilesDir().getPath() + File.separator + "Android" + File.separator + "data" + File.separator + SwitchAppTool.APPLICATION_ID + File.separator + "cache" + File.separator);
        THUMBPATH = rootPath + "thumb" + File.separator;
        SRCPATH = rootPath + "src" + File.separator;
        File src = new File(SRCPATH), thumb = new File(THUMBPATH);
        if (!src.exists()) {
            src.mkdirs();
        }
        Log.e(TAG, src.getPath());
        if (!thumb.exists()) {
            thumb.mkdirs();
        }
        Log.e(TAG, thumb.getPath());
    }

    private static FileUtil instance;

    public static FileUtil getInstance(Context context) {
        if (instance == null) {
            instance = new FileUtil(context);
        }
        return instance;
    }

    /**
     * 写文件到sdcard
     *
     * @param fileName
     * @param bm
     * @param isThumb
     * @return
     */
    public boolean writeFileToSD(String fileName, Bitmap bm, boolean isThumb) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);// png类型
        byte[] datas = baos.toByteArray();
        return writeFileToSD(fileName, datas, isThumb);
    }

    /**
     * 将文件写入SD卡
     */
    public boolean writeFileToSD(String fileName, byte[] datas, boolean isThumb) {
        try {
            File file = new File((isThumb ? THUMBPATH : SRCPATH) + fileName);
            Log.e(TAG, "writeFileToSD[]:" + file.getPath());
            if (!file.exists()) {
                file.createNewFile();// 创建文件
            }
            FileOutputStream stream = new FileOutputStream(file);
            stream.write(datas);
            stream.close();

            return true;
        } catch (Exception e) {
            Log.e(TAG, "writeFileToSD[]:Error" + e.getMessage());
        }
        return false;
    }

    /**
     * 判断sdcard有没有文件或文件夹
     *
     * @param fileName
     * @return
     */
    public boolean existsSrc(String fileName) {
        boolean flag = new File(SRCPATH + fileName).exists();
        Log.e(TAG, "existsSrc[]:" + SRCPATH + fileName + ";result:" + flag);
        return flag;
    }


    private static CustomProgress customProgress = null;

    public boolean clear(Context context) {
        customProgress = CustomProgress.show(context, "loading...", false, null);
        File srcDir = new File(SRCPATH);

        String[] children = srcDir.list();
        for (int i = 0; i < children.length; i++) {
            if (!new File(srcDir, children[i]).delete()) {
                return false;
            }
        }
        File thumbDir = new File(THUMBPATH);
        children = thumbDir.list();
        for (int i = 0; i < children.length; i++) {
            if (!new File(thumbDir, children[i]).delete()) {
                return false;
            }
        }
        customProgress.dismiss();
        return true;
    }

    public boolean existsThumb(String fileName) {
        boolean flag = new File(THUMBPATH + fileName).exists();
        Log.e(TAG, "existsThumb[]:" + THUMBPATH + fileName + ";result:" + flag);
        return flag;
    }

    /**
     * 读取文件
     */
    private Bitmap readSDFile(String fileName, boolean isThumb) {
        String pathName = (isThumb ? THUMBPATH : SRCPATH) + fileName;
        Bitmap bitmap = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        // 获取这个图片的宽和高,注意此处的bitmap为null
        bitmap = BitmapFactory.decodeFile(pathName, options);
        options.inJustDecodeBounds = false; // 设为 false

        // 重新读入图片,读取缩放后的bitmap,注意这次要把options.inJustDecodeBounds 设为 false
        bitmap = BitmapFactory.decodeFile(pathName, options);
        // 利用ThumbnailUtils来创建缩略图,这里要指定要缩放哪个Bitmap对象

        return bitmap;
    }

    public Bitmap readSrcFile(String fileName) {
        return readSDFile(fileName, false);
    }

    public Bitmap readThumbFile(String fileName) {
        return readSDFile(fileName, true);
    }


    public String getSRCPATH() {
        return SRCPATH;
    }

    public String getTHUMBPATH() {
        return THUMBPATH;
    }
}


转载于:https://my.oschina.net/u/2320057/blog/632254

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值