FileUtils.java 2.45 KB
package com.polysoft.nafmii.utils;

import android.content.Context;
import android.net.Uri;
import android.os.Environment;
import android.text.TextUtils;

import java.io.File;

public class FileUtils {

    public static File getIndex() {
        return WebVersionUtils.getIndex();
    }

    public static File getDistDir() {
        return WebVersionUtils.getDistDir();
    }

    public static File getDownloadDir() {
        File download = new File(getRootDir(), "download");
        if (!download.exists()) {
            download.mkdirs();
        }
        return download;
    }

    public static File getSysDownLoadDir() {
        return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
//        return CtxUtils.getApplication().getExternalFilesDir();
    }

    public static File getRootDir() {
        return CtxUtils.getApplication().getFilesDir();
    }

    // 获取热跟新目录
    public static File getHotDir() {
        File hot = new File(getRootDir(), "Hot");
        if (!hot.exists()) {
            hot.mkdirs();
        }
        return hot;
    }

    public static File getRootCacheDir() {
        return CtxUtils.getApplication().getCacheDir();
    }

    public static void deletes(File file) {
        if (file.isDirectory()) {
            for (File f : file.listFiles()) {
                deletes(f);
            }
        }
        file.delete();
    }

    public static String getFileName(String name) {
        int nameMaxLength = 60;
        if (!TextUtils.isEmpty(name)) {
            if (name.length() > nameMaxLength) {
                String newname = name.substring(0, nameMaxLength);
                String[] split = name.split("\\.");
                if (split.length > 1) {
                    return newname + "." + split[split.length - 1];
                }
                return newname;
            }
            return name;
        }
        return "";
    }

    public static String getUrlFileName(String url) {
        if (!TextUtils.isEmpty(url)) {
            String[] split = url.split("/");
            if (split.length > 1) {
                String name = split[split.length - 1];
                return getFileName(name);
            }
        }
        return "";
    }

    public static void openFile(Context context, File file) {
        if (!file.exists()) {
            return;
        }
        Uri uri = FileProviderUtils.getUriForFile(context, file);
        IntentUtils.startOpenFile(context, uri);
    }
}