/*
* Copyright 2017 GcsSloop
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Last modified 2017-03-08 01:01:18
*
* GitHub: https://github.com/GcsSloop
* Website: http://www.gcssloop.com
* Weibo: http://weibo.com/GcsSloop
*/
package com.gcssloop.diycode.utils;
import android.content.Context;
import android.os.Environment;
import java.io.File;
public class FileUtil {
private FileUtil() {
}
//****系统文件目录**********************************************************************************************
/**
* @return 程序系统文件目录
*/
public static String getFileDir(Context context) {
return String.valueOf(context.getFilesDir());
}
/**
* @param context 上下文
* @param customPath 自定义路径
* @return 程序系统文件目录绝对路径
*/
public static String getFileDir(Context context, String customPath) {
String path = context.getFilesDir() + formatPath(customPath);
mkdir(path);
return path;
}
//****系统缓存目录**********************************************************************************************
/**
* @return 程序系统缓存目录
*/
public static String getCacheDir(Context context) {
return String.valueOf(context.getCacheDir());
}
/**
* @param context 上下文
* @param customPath 自定义路径
* @return 程序系统缓存目录
*/
public static String getCacheDir(Context context, String customPath) {
String path = context.getCacheDir() + formatPath(customPath);
mkdir(path);
return path;
}
//****Sdcard文件目录**********************************************************************************************
/**
* @return 内存卡文件目录
*/
public static String getExternalFileDir(Context context) {
return String.valueOf(context.getExternalFilesDir(""));
}
/**
* @param context 上下文
* @param customPath 自定义路径
* @return 内存卡文件目录
*/
public static String getExternalFileDir(Context context, String customPath) {
String path = context.getExternalFilesDir("") + formatPath(customPath);
mkdir(path);
return path;
}
//****Sdcard缓存目录**********************************************************************************************
/**
* @return 内存卡缓存目录
*/
public static String getExternalCacheDir(Context context) {
return String.valueOf(context.getExternalCacheDir());
}
/**
* @param context 上下文
* @param customPath 自定义路径
* @return 内存卡缓存目录
*/
public static String getExternalCacheDir(Context context, String customPath) {
String path = context.getExternalCacheDir() + formatPath(customPath);
mkdir(path);
return path;
}
//****公共文件夹**********************************************************************************************
/**
* @return 公共下载文件夹
*/
public static String getPublicDownloadDir() {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
}
//****相关工具**********************************************************************************************
/**
* 创建文件夹
*
* @param DirPath 文件夹路径
*/
public static void mkdir(String DirPath) {
File file = new File(DirPath);
if (!(file.exists() && file.isDirectory())) {
file.mkdirs();
}
}
/**
* 格式化文件路径
* 示例: 传入 "sloop" "/sloop" "sloop/" "/sloop/"
* 返回 "/sloop"
*/
private static String formatPath(String path) {
if (!path.startsWith("/"))
path = "/" + path;
while (path.endsWith("/"))
path = new String(path.toCharArray(), 0, path.length() - 1);
return path;
}
/**
* @return 存储卡是否挂载(存在)
*/
public static boolean isMountSdcard() {
String status = Environment.getExternalStorageState();
return status.equals(Environment.MEDIA_MOUNTED);
}
}