FileUtil.java 3.91 KB
Newer Older
ydl committed
1 2 3 4 5 6 7 8
package com.yidianling.user.mine;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.os.StatFs;
import android.provider.MediaStore;
YKai committed
9
import androidx.loader.content.CursorLoader;
ydl committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144


import com.ydl.ydlcommon.base.BaseApp;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * 文件工具类
 * Created by Dog on 2015/5/15.
 */
public class FileUtil {

    /**
     * 获取本地文件夹路径
     * @param dir 文件夹名
     * @return 文件夹的完整路径
     */
    public static String getLocalStorePath(String dir) {
        if(BaseApp.Companion.getApp() == null){
            throw new IllegalArgumentException("app未初始化");
        }

        String path;

        if (isExternalStorageExist() && getSDFreeSize() >= 10) {
            path = BaseApp.Companion.getApp().getExternalCacheDir().getPath() + "/" + dir + "/";
        } else {
            path = BaseApp.Companion.getApp().getCacheDir().getPath() + "/" + dir + "/";
        }

        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }

        return path;
    }

    /**
     * 判断SD卡是否存在
     *
     * @return boolean
     */
    public static boolean ExistSDCard() {
        return Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
    }

    public static byte[] getBytesFromFile(File f){
        if (f == null){
            return null;
        }
        try{
            FileInputStream stream = new FileInputStream(f);
            ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = stream.read(b)) != -1)
                out.write(b, 0, n);
            stream.close();
            out.close();
            return out.toByteArray();
        } catch (IOException ignore){
        }
        return null;
    }

    /**
     * 判断有无外部存储
     * @return true 存在,false 不存在
     */
    public static boolean isExternalStorageExist(){
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    /**
     * 判断SD卡剩余容量,单位MB
     *
     * @return long
     */
    public static long getSDFreeSize() {
        // 取得SD卡文件路径
        File path = Environment.getExternalStorageDirectory();
        StatFs sf = new StatFs(path.getPath());
        // 获取单个数据块的大小(Byte)
        long blockSize = sf.getBlockSize();
        // 空闲的数据块的数量
        long freeBlocks = sf.getAvailableBlocks();
        // 返回SD卡空闲大小
        return (freeBlocks * blockSize) / 1024 / 1024; // 单位MB
    }

    /**
     * 通过Uri获取File
     * @param context 上下文
     * @param uri uri地址
     * @return file文件对象
     */
    public static String getFilePathByUri(Context context, Uri uri){
        String[] proj = { MediaStore.Images.Media.DATA };
        CursorLoader loader = new CursorLoader(context, uri, proj, null, null, null);
        Cursor cursor = loader.loadInBackground();
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

    /**
     * 删除一个人文件夹
     * @param file 文件夹file对象
     */
    public static void dfsToDeleteDir(File file){
        if(file.isFile()){
            file.delete();
            return;
        }

        if(file.isDirectory()){
            File[] childFiles = file.listFiles();
            if(childFiles == null || childFiles.length == 0){
                file.delete();
                return;
            }

            for (File childFile : childFiles) {
                dfsToDeleteDir(childFile);
            }

            file.delete();
        }
    }
}