package com.xjf.repository.utils;
import android.os.Environment;
import android.os.StatFs;
import java.io.File;
import static com.xjf.repository.utils.ConstUtils.*;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2016/8/11
* desc : SD卡相关工具类
* </pre>
*/
public class SDCardUtils {
private SDCardUtils() {
throw new UnsupportedOperationException("u can't fuck me...");
}
/**
* 判断SD卡是否可用
*
* @return true : 可用<br>false : 不可用
*/
public static boolean isSDCardEnable() {
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
}
/**
* 获取SD卡路径
* <p>一般是/storage/emulated/0/</p>
*
* @return SD卡路径
*/
public static String getSDCardPath() {
return Environment.getExternalStorageDirectory().getPath() + File.separator;
}
/**
* 获取SD卡Data路径
*
* @return Data路径
*/
public static String getDataPath() {
return Environment.getDataDirectory().getPath();
}
/**
* 计算SD卡的剩余空间
*
* @param unit <ul>
* <li>{@link MemoryUnit#BYTE}: 字节</li>
* <li>{@link MemoryUnit#KB} : 千字节</li>
* <li>{@link MemoryUnit#MB} : 兆</li>
* <li>{@link MemoryUnit#GB} : GB</li>
* </ul>
* @return 返回-1,说明SD卡不可用,否则返回SD卡剩余空间
*/
public static double getFreeSpace(MemoryUnit unit) {
if (isSDCardEnable()) {
try {
StatFs stat = new StatFs(getSDCardPath());
long blockSize, availableBlocks;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
availableBlocks = stat.getAvailableBlocksLong();
blockSize = stat.getBlockSizeLong();
} else {
availableBlocks = stat.getAvailableBlocks();
blockSize = stat.getBlockSize();
}
return FileUtils.byte2Size(availableBlocks * blockSize, unit);
} catch (Exception e) {
e.printStackTrace();
return -1.0;
}
} else {
return -1.0;
}
}
/**
* 获取SD卡的状态
*/
public static String getState() {
return Environment.getExternalStorageState();
}
/**
* SD卡是否可用
*
* @return 只有当SD卡已经安装并且准备好了才返回true
*/
public static boolean isAvailable() {
return getState().equals(Environment.MEDIA_MOUNTED);
}
/**
* 获取SD卡的根目录
*
* @return null:不存在SD卡
*/
public static File getRootDirectory() {
return isAvailable() ? Environment.getExternalStorageDirectory() : null;
}
/**
* 获取SD卡的根路径
*
* @return null:不存在SD卡
*/
public static String getRootPath() {
File rootDirectory = getRootDirectory();
return rootDirectory != null ? rootDirectory.getPath() : null;
}
/**
* 获取sd卡路径
*
* @return Stringpath
*/
public static String getSDPath() {
File sdDir = null;
boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); //判断sd卡是否存在
if (sdCardExist) {
sdDir = Environment.getExternalStorageDirectory();//获取跟目录
}
return sdDir.toString();
}
}