337 lines
9.5 KiB
Kotlin
337 lines
9.5 KiB
Kotlin
package com.img.rabbit.utils;
|
|
|
|
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.os.Environment;
|
|
import android.os.StatFs;
|
|
import android.text.TextUtils;
|
|
import android.util.Log;
|
|
|
|
import com.img.rabbit.provider.utils.HeadParamUtils;
|
|
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.text.DecimalFormat;
|
|
import java.util.Objects;
|
|
|
|
@SuppressWarnings({"ResultOfMethodCallIgnored", "CallToPrintStackTrace"})
|
|
public class FileUtils {
|
|
|
|
private static FileUtils instance;
|
|
private static String packageName = "dev";
|
|
|
|
// 文件缓存路径
|
|
private String CACHE_DIR;
|
|
|
|
// 下载目录
|
|
private File downloadDir;
|
|
// 缓存目录
|
|
private File cacheDir;
|
|
// 图片缓存目录
|
|
private File cacheImageDir;
|
|
|
|
private File cacheOriginalImageDir;
|
|
|
|
private File cacheEditImageDir;
|
|
|
|
private File cachePuzzleImageDir;
|
|
// 下载Uni小程序WGTD文件存放目录
|
|
private File uniWGTDir;
|
|
|
|
public static FileUtils getInstance() {
|
|
if (instance == null) {
|
|
synchronized (FileUtils.class) {
|
|
if (instance == null) {
|
|
instance = new FileUtils(Objects.requireNonNull(HeadParamUtils.INSTANCE.getApplicationContext()));
|
|
}
|
|
}
|
|
}
|
|
return instance;
|
|
}
|
|
|
|
private FileUtils(Context context) {
|
|
CACHE_DIR = Objects.requireNonNull(context.getExternalFilesDir(null)).getAbsolutePath() + File.separator + packageName + File.separator;
|
|
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
|
|
cacheDir = new File(CACHE_DIR, "/cache");
|
|
} else {
|
|
cacheDir = context.getCacheDir();
|
|
}
|
|
if (!cacheDir.exists())
|
|
cacheDir.mkdirs();
|
|
cacheImageDir = new File(cacheDir, "/image/");
|
|
if (!cacheImageDir.exists())
|
|
cacheImageDir.mkdirs();
|
|
|
|
cacheOriginalImageDir = new File(cacheDir, "/originalImage/");
|
|
if (!cacheOriginalImageDir.exists())
|
|
cacheOriginalImageDir.mkdirs();
|
|
|
|
cacheEditImageDir = new File(cacheDir, "/cacheEditImage/");
|
|
if (!cacheEditImageDir.exists())
|
|
cacheEditImageDir.mkdirs();
|
|
|
|
cachePuzzleImageDir = new File(cacheDir, "/cachePuzzleImage/");
|
|
if (!cachePuzzleImageDir.exists())
|
|
cachePuzzleImageDir.mkdirs();
|
|
|
|
downloadDir = new File(cacheDir, "/download/");
|
|
if (!downloadDir.exists())
|
|
downloadDir.mkdirs();
|
|
|
|
uniWGTDir = new File(cacheDir, "/uniApp/");
|
|
if (!uniWGTDir.exists())
|
|
uniWGTDir.mkdirs();
|
|
}
|
|
|
|
public String getCACHE_DIR() {
|
|
return CACHE_DIR;
|
|
}
|
|
|
|
/**
|
|
* 获取缓存目录
|
|
*/
|
|
public File getCacheDir() {
|
|
return cacheDir;
|
|
}
|
|
|
|
/**
|
|
* 获取下载目录
|
|
*/
|
|
public File getCacheDownLoadDir() {
|
|
return downloadDir;
|
|
}
|
|
|
|
/**
|
|
* 获取UniApp目录
|
|
*/
|
|
public File getCacheUniAppDir() {
|
|
return uniWGTDir;
|
|
}
|
|
|
|
/**
|
|
* 获取缓存图片目录
|
|
*/
|
|
public File getCacheImageDir() {
|
|
return cacheImageDir;
|
|
}
|
|
|
|
/**
|
|
* 水印照片编辑后的路径
|
|
*/
|
|
public File getCacheEditImageDir() {
|
|
return cacheEditImageDir;
|
|
}
|
|
|
|
public File getCacheOriginalImageDir() {
|
|
return cacheOriginalImageDir;
|
|
}
|
|
|
|
/**
|
|
* 拼图路径
|
|
*/
|
|
public File getCachePuzzleImageDir() {
|
|
return cachePuzzleImageDir;
|
|
}
|
|
|
|
|
|
/**
|
|
* 创建一个临时图片文件
|
|
*/
|
|
public File newTempImageFile() {
|
|
return new File(cacheImageDir, System.currentTimeMillis() + ".jpg");
|
|
}
|
|
|
|
/**
|
|
* 判断是否安装SD卡
|
|
*/
|
|
public static boolean checkSaveLocationExists() {
|
|
String sDCardStatus = Environment.getExternalStorageState();
|
|
boolean status;
|
|
status = sDCardStatus.equals(Environment.MEDIA_MOUNTED);
|
|
return status;
|
|
}
|
|
|
|
/**
|
|
* 删除指定目录下文件及目录
|
|
*/
|
|
public static void deleteFolderFile(String filePath) {
|
|
if (!TextUtils.isEmpty(filePath)) {
|
|
try {
|
|
File file = new File(filePath);
|
|
if (file.isDirectory()) {// 处理目录
|
|
File[] files = file.listFiles();
|
|
for (int i = 0; i < Objects.requireNonNull(files).length; i++) {
|
|
deleteFolderFile(files[i].getAbsolutePath());
|
|
}
|
|
}
|
|
if (!file.isDirectory()) {// 如果是文件,删除
|
|
file.delete();
|
|
} else {// 目录
|
|
if (Objects.requireNonNull(file.listFiles()).length == 0) {// 目录下没有文件或者目录,删除
|
|
file.delete();
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
Log.e("FileUtils", Objects.requireNonNull(e.getMessage()));
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除文件
|
|
*/
|
|
public boolean deleteFile(String path) {
|
|
boolean status;
|
|
SecurityManager checker = new SecurityManager();
|
|
|
|
if (!path.isEmpty()) {
|
|
File newPath = new File(path);
|
|
checker.checkDelete(newPath.toString());
|
|
if (newPath.isFile()) {
|
|
try {
|
|
|
|
newPath.delete();
|
|
status = true;
|
|
} catch (SecurityException se) {
|
|
Log.e("FileUtils", Objects.requireNonNull(se.getMessage()));
|
|
status = false;
|
|
}
|
|
} else
|
|
status = false;
|
|
} else
|
|
status = false;
|
|
return status;
|
|
}
|
|
|
|
/**
|
|
* 获取目录文件大小
|
|
*/
|
|
public static long getDirSize(File dir) {
|
|
if (dir == null) {
|
|
return 0;
|
|
}
|
|
if (!dir.isDirectory()) {
|
|
return 0;
|
|
}
|
|
long dirSize = 0;
|
|
File[] files = dir.listFiles();
|
|
for (File file : Objects.requireNonNull(files)) {
|
|
if (file.isFile()) {
|
|
dirSize += file.length();
|
|
} else if (file.isDirectory()) {
|
|
dirSize += file.length();
|
|
dirSize += getDirSize(file); // 递归调用继续统计
|
|
}
|
|
}
|
|
return dirSize;
|
|
}
|
|
|
|
/**
|
|
* 获取指定文件大小
|
|
*/
|
|
@SuppressWarnings("resource")
|
|
public static long getFileSize(File file) throws Exception {
|
|
long size = 0;
|
|
if (file.exists()) {
|
|
FileInputStream fis = null;
|
|
fis = new FileInputStream(file);
|
|
size = fis.available();
|
|
} else {
|
|
|
|
Log.e("获取文件大小", "文件不存在!");
|
|
}
|
|
return size;
|
|
}
|
|
|
|
/**
|
|
* 获取指定文件夹
|
|
*/
|
|
public static long getFileSizes(File f) throws Exception {
|
|
long size = 0;
|
|
File[] flist = f.listFiles();
|
|
for (int i = 0; i < Objects.requireNonNull(flist).length; i++) {
|
|
if (flist[i].isDirectory()) {
|
|
size = size + getFileSizes(flist[i]);
|
|
} else {
|
|
size = size + getFileSize(flist[i]);
|
|
}
|
|
}
|
|
return size;
|
|
}
|
|
|
|
/**
|
|
* 转换文件大小
|
|
*/
|
|
public static String toFileSize(long fileS) {
|
|
DecimalFormat df = new DecimalFormat("#.00");
|
|
String fileSizeString;
|
|
String wrongSize = "0M";
|
|
if (fileS == 0) {
|
|
return wrongSize;
|
|
}
|
|
if (fileS < 1024) {
|
|
fileSizeString = df.format((double) fileS) + "B";
|
|
} else if (fileS < 1048576) {
|
|
fileSizeString = df.format((double) fileS / 1024) + "K";
|
|
} else if (fileS < 1073741824) {
|
|
fileSizeString = df.format((double) fileS / 1048576) + "M";
|
|
} else {
|
|
fileSizeString = df.format((double) fileS / 1073741824) + "G";
|
|
}
|
|
return fileSizeString;
|
|
}
|
|
|
|
//判断文件是否存在
|
|
public boolean fileIsExists(File strFile) {
|
|
try {
|
|
if (!strFile.exists()) {
|
|
return false;
|
|
}
|
|
} catch (Exception e) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
//判断文件是否存在
|
|
public boolean fileIsExists(String strFile) {
|
|
try {
|
|
File f = new File(strFile);
|
|
if (!f.exists()) {
|
|
return false;
|
|
}
|
|
} catch (Exception e) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
//外部存储空间
|
|
public static long getExternalStorageSpace() {
|
|
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
|
|
StatFs externalStatFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
|
|
long externalBlockSize = externalStatFs.getBlockSizeLong();
|
|
long externalTotalSize = externalStatFs.getBlockCountLong() * externalBlockSize;
|
|
long externalAvailableSize = externalStatFs.getAvailableBlocksLong() * externalBlockSize;
|
|
Log.d("FileUtils", "当前外部空间总大小----" + externalTotalSize);
|
|
Log.d("FileUtils", "当前外部空间可用大小----" + externalAvailableSize);
|
|
return externalAvailableSize;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 打开图库
|
|
*/
|
|
public void openGallery(Context context) {
|
|
try {
|
|
Intent intent = new Intent(Intent.ACTION_MAIN);
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
intent.addCategory(Intent.CATEGORY_APP_GALLERY);
|
|
context.startActivity(intent);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
} |