SD卡工具类整理:

 package com.gzcivil.utils;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal; import android.os.Environment; /**
* SD卡工具类
*/
public class SDTool {
// 字节参考量(bt/KB/MB/GB/TB)
protected final long SIZE_BT = 1024L;
protected final long SIZE_KB = SIZE_BT * 1024L;
protected final long SIZE_MB = SIZE_KB * 1024L;
protected final long SIZE_GB = SIZE_MB * 1024L;
protected final long SIZE_TB = SIZE_GB * 1024L;
protected final int SACLE = 2; private static SDTool mSDTool;
private final String mModuleName = this.getClass().getSimpleName();
private static boolean SDCardAvailable = false; // SDCard有效状态
private static String SDCardRootDir = ""; // SDCard路径 private SDTool() {
super();
LogUtils.i(SysUtils.LOG_TAG, "创建文件夹");
this.initSDTool();
} public static synchronized SDTool getInstance() {
if (mSDTool == null)
mSDTool = new SDTool();
return mSDTool;
} // --------------------------get some dir path....
public String getBaseDir() {
return SDCardAvailable ? SDCardRootDir + SysConstants.PATH_ROOT : null;
} /** 获取图片sdcard保存路径 */
public String getImgDir(String urlFileName) {
if (SDCardAvailable)
return SDCardRootDir + SysConstants.PATH_IMAGE + urlFileName;
return null;
} /** 获取上传图片sdcard保存路径 */
public String getUpImgDir(String urlFileName) {
if (SDCardAvailable)
return SDCardRootDir + SysConstants.PATH_UPIMAGE + urlFileName;
return null;
} /** 获取sdcard临时目录的路径 */
public String getTempBase() {
if (SDCardAvailable)
return SDCardRootDir + SysConstants.PATH_TEMP;
return null;
} /** 获取sdcard下载目录的路径 */
public String getDownloadBase() {
if (SDCardAvailable)
return SDCardRootDir + SysConstants.PATH_DOWNLOAD;
return null;
} /** 获取sdcard数据库目录的路径 */
public String getDataBase() {
if (SDCardAvailable)
return SDCardRootDir + SysConstants.PATH_DATABASE;
return null;
} /** 获取文件在sdcard临时目录的保存路径 */
public String getTempDir(String urlFileName) {
if (SDCardAvailable)
return SDCardRootDir + SysConstants.PATH_TEMP + urlFileName;
return null;
} /** 获取文件在sdcard临时目录的保存路径 */
public String getTempDirUpImage(String urlFileName) {
if (SDCardAvailable)
return SDCardRootDir + SysConstants.PATH_UPIMAGE;
return null;
} /**
* 根据SD卡相对路径返回绝对路径
*
* @param filePath
* @return
*/
public String getSdFullPath(String filePath) {
return SDCardRootDir + filePath;
} // --------------------------public function....
/**
* SD卡可用状态检测
*/
public static boolean isAvailable() {
return SDCardAvailable;
} /**
* 检测文件是否存在
*/
public boolean exists(String filePath) {
return SDCardAvailable ? new File(filePath).exists() : false;
} /**
* 检测是否为零字节文件
*/
public boolean emptyFile(String filePath) {
return (!SDCardAvailable || new File(filePath).length() < 1) ? true : false;
} /**
* 根据文件名删除文件
*
* @param filePath
* SDCard卡上完整路径
* @return void
*/
public void deleteFile(String filePath) {
if (!SDCardAvailable || StringUtils.isEmpty(filePath))
return;
File file = new File(filePath);
if (file.exists())
file.delete();
} /**
* 删除指定目录下所有文件及子目录(不包含自身)
*
* @param dirPath
* @return
* @return void
*/
public synchronized boolean deleteDir(String dirPath) {
boolean clearFlag = false;
if (!SDCardAvailable) {
return false;
} File file = new File(dirPath);
if (file.exists() && file.isDirectory()) {
File[] files = file.listFiles();
for (File fileItem : files) {
if (fileItem.isFile()) {
fileItem.delete();
}
}
clearFlag = true;
}
return clearFlag;
} /**
* 获取指定目录大小
*
* @param dirPath
* @return
*/
public long getPathSize(String dirPath) {
long size = 0;
if (!SDCardAvailable)
return size; File file = new File(dirPath);
if (file.exists() && file.isDirectory()) {
File[] files = file.listFiles();
for (File fileItem : files) {
if (fileItem.isFile()) {
size += fileItem.length();
}
}
}
return size;
} /**
* 读取SD卡中文本文件
*
* @param fileName
* @return
*/ public String readSDFile(String fileName) {
StringBuffer sb = new StringBuffer();
try {
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
int c;
while ((c = fis.read()) != -1) {
sb.append((char) c);
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
} /**
* 获取指定目录大小-格式化好字串
*
* @param dirPath
* @return
*/
public String sizeFormatString(long size) {
String sizeFormat = "";
if (size >= 0 && size < SIZE_BT) {
sizeFormat = size + " Byte";
} else if (size >= SIZE_BT && size < SIZE_KB) {
sizeFormat = size / SIZE_BT + " KB";
} else if (size >= SIZE_KB && size < SIZE_MB) {
sizeFormat = size / SIZE_KB + " MB";
} else if (size >= SIZE_MB && size < SIZE_GB) {
BigDecimal longs = new BigDecimal(Double.valueOf(size + "").toString());
BigDecimal sizeMB = new BigDecimal(Double.valueOf(SIZE_MB + "").toString());
String result = longs.divide(sizeMB, SACLE, BigDecimal.ROUND_HALF_UP).toString();
// double result=size/(double)SIZE_MB;
sizeFormat = result + " GB";
} else {
BigDecimal longs = new BigDecimal(Double.valueOf(size + "").toString());
BigDecimal sizeMB = new BigDecimal(Double.valueOf(SIZE_GB + "").toString());
String result = longs.divide(sizeMB, SACLE, BigDecimal.ROUND_HALF_UP).toString();
sizeFormat = result + " TB";
}
return sizeFormat;
} /**
* 文件拷贝
*
* @param fromFile
* @param toFile
* @return
*/
public boolean CopySdcardFile(String fromFile, String toFile) {
boolean copyState = false;
try {
InputStream fosfrom = new FileInputStream(fromFile);
OutputStream fosto = new FileOutputStream(toFile);
byte bt[] = new byte[1024];
int c;
while ((c = fosfrom.read(bt)) > 0) {
fosto.write(bt, 0, c);
}
fosfrom.close();
fosto.close();
copyState = true;
} catch (Exception ex) {
LogUtils.e(SysUtils.LOG_TAG, "CopySdcardFile Exception: fromFile=" + fromFile + " toFile=" + toFile + " 复制失败.", this.mModuleName);
}
return copyState;
} // ==============================================
protected String getMd5(String urlName) {
return StringUtils.md5(urlName.getBytes());
} @SuppressWarnings("static-access")
private void initSDTool() {
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return;
}
SDCardAvailable = true;
SDCardRootDir = Environment.getExternalStorageDirectory().getPath(); this.initSDDir(SDCardRootDir + SysConstants.PATH_ROOT);
this.initSDDir(SDCardRootDir + SysConstants.PATH_IMAGE);
this.initSDDir(SDCardRootDir + SysConstants.PATH_DATABASE);
this.initSDDir(SDCardRootDir + SysConstants.PATH_DOWNLOAD);
this.initSDDir(SDCardRootDir + SysConstants.PATH_TEMP);
this.initSDDir(SDCardRootDir + SysConstants.PATH_UPIMAGE);
} /**
* 获取SDCard根路径
*/
public static String getSDCardRootDir() {
return SDCardRootDir;
} public static boolean initSDDir(String dirPath) {
boolean flag = false;
if (SDCardAvailable) {
File tempFile = new File(dirPath);
if (!tempFile.exists()) {
if (tempFile.mkdirs()) {
flag = true;
LogUtils.v(SysUtils.LOG_TAG, "tempFile=" + dirPath + " mkdir success!");
}
} else {
flag = true;
LogUtils.v(SysUtils.LOG_TAG, "tempFile=" + dirPath + " is exist!");
}
}
return flag;
} }
04-21 05:04