/**
* SD卡是否存在
*
* @return true 存在 false 不存在
*/
public boolean isSdExist() {
boolean sdCardExist = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
return sdCardExist;
}
/**
* 获得SD卡总大小
* @parm sizeType: 返回的SD卡大小的单位 SIZETYPE_B,SIZETYPE_KB,SIZETYPE_MB,SIZETYPE_GB
* @return double类型 SD卡大小
*/
public double getSDTotalSize(int sizeType) {
if (isSdExist()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return FormetFileSize(blockSize * totalBlocks, sizeType);
}
return 0;
}
/**
* 获得SD卡总大小
* @parm context 上下文
* @return
*/
public String getSDTotalSize(Context context) {
if (isSdExist()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return Formatter.formatFileSize(context, blockSize * totalBlocks);
}
return "";
}
/**
* 获得SD卡剩余容量,即可用大小
*
* @return
*/
public double getSDAvailableSize(int sizeType) {
if (isSdExist()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return FormetFileSize(blockSize * availableBlocks, sizeType);
}
return 0;
}
/**
* 获得SD卡剩余容量,即可用大小
*
* @return
*/
public String getSDAvailableSize(Context context) {
if (isSdExist()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return Formatter.formatFileSize(context, blockSize * availableBlocks);
}
return "";
}
/**
* 获取指定文件大小
*
* @param file
* @return
* @throws Exception
*/
@SuppressWarnings("resource")
public long getFileSize(File file) throws Exception {
long size = 0;
if (file.exists()) {
FileInputStream fis = null;
fis = new FileInputStream(file);
size = fis.available();
} else {
file.createNewFile();
Log.e("获取文件大小", "文件不存在!");
}
return size;
}
/**
* 获取指定文件夹大小
*
* @param f
* @return
* @throws Exception
*/
public long getFileSizes(File f) throws Exception {
long size = 0;
File flist[] = f.listFiles();
for (int i = 0; i < flist.length; i++) {
if (flist[i].isDirectory()) {
size = size + getFileSizes(flist[i]);
} else {
size = size + getFileSize(flist[i]);
}
}
return size;
}
/**
* 获取指定文件的指定单位的大小
*
* @param filePath 文件路径
* @param sizeType 获取大小的类型1为B、2为KB、3为MB、4为GB
* @return double值的大小
*/
public double getFileOrFilesSize(String filePath, int sizeType) {
File file = new File(filePath);
if (!file.exists()) {
return 0;
}
long blockSize = 0;
try {
if (file.isDirectory()) {
blockSize = getFileSizes(file);
} else {
blockSize = getFileSize(file);
}
} catch (Exception e) {
e.printStackTrace();
Log.e("获取文件大小", "获取失败!");
}
return FormetFileSize(blockSize, sizeType);
}
public static final int SIZETYPE_B = 1;
public static final int SIZETYPE_KB = 2;
public static final int SIZETYPE_MB = 3;
public static final int SIZETYPE_GB = 4;
/**
* 转换文件大小,指定转换的类型
*
* @param fileS
* @param sizeType
* @return
*/
private double FormetFileSize(long fileS, int sizeType) {
DecimalFormat df = new DecimalFormat("#.00");
double fileSizeLong = 0;
switch (sizeType) {
case SIZETYPE_B:
fileSizeLong = Double.valueOf(df.format((double) fileS));
break;
case SIZETYPE_KB:
fileSizeLong = Double.valueOf(df.format((double) fileS / 1024));
break;
case SIZETYPE_MB:
fileSizeLong = Double.valueOf(df.format((double) fileS / 1048576));
break;
case SIZETYPE_GB:
fileSizeLong = Double.valueOf(df
.format((double) fileS / 1073741824));
break;
default:
break;
}
return fileSizeLong;
}
/**
* 创建文件
*
* @param path 文件夹路径
* @param fileName 文件名称
* @return
*/
public File getOutFile(String path, String fileName) {
if (!isSdExist()) {
return null;
}
if (path != null) {
File mediaStorageDir = new File(path);
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
return null;
}
}
}
File f = new File(path + fileName);
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return f;
}
/**
* 向已创建的文件中写入数据
* @param str 写入内容
* @param fileName 文件名称
*/
@SuppressLint("SimpleDateFormat")
public void print(String str, String fileName) {
// 获取SD卡剩余大小
double sdSize = getSDAvailableSize();
if (sdSize < 3) {
return;
}
FileWriter fw = null;
BufferedWriter bw = null;
String datetime = "";
try {
SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd" + " " + "hh:mm:ss");
datetime = tempDate.format(new java.util.Date()).toString();//插入日期时间
fw = new FileWriter(filenameTemp, true);//
// 创建FileWriter对象,用来写入字符流
bw = new BufferedWriter(fw); // 将缓冲对文件的输出
String myreadline = "[ " + datetime + "]" + str + "\n";
bw.write(myreadline); // 写入文件
bw.newLine();
bw.flush(); // 刷新该流的缓冲
bw.close();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
bw.close();
fw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
}
}
}
/**
* 获得机身内存总大小
*
* @return
*/
public String getRomTotalSize(Context context) {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return Formatter.formatFileSize(context, blockSize * totalBlocks);
}
/**
* 获得机身内存总大小
* @parm sizeType 返回大小的单位
* @return
*/
public double getRomTotalSize(int sizeType) {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return FormetFileSize(blockSize * totalBlocks , sizeType);
}
/**
* 获得机身可用内存
*
* @return
*/
private String getRomAvailableSize(Context context) {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return Formatter.formatFileSize(context, blockSize * availableBlocks);
}
/**
* 获得机身可用内存
*
* @return
*/
private double getRomAvailableSize(inte sizeType) {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return FormetFileSize(blockSize * availableBlocks , sizeType);
}
原文:http://my.oschina.net/u/942298/blog/528561