Skip to content

Commit

Permalink
增强SDutils
Browse files Browse the repository at this point in the history
  • Loading branch information
马天宇 committed Apr 20, 2015
1 parent 05afb7c commit 77af9f4
Showing 1 changed file with 118 additions and 10 deletions.
128 changes: 118 additions & 10 deletions src/com/litesuits/common/utils/SdCardUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import android.os.StatFs;
import com.litesuits.android.log.Log;

import java.io.File;
import java.io.*;

/**
* @author MaTianyu
Expand All @@ -22,6 +22,114 @@ public static StatFs getStatFs(String path) {
return new StatFs(path);
}

/**
* 获取手机自身内存路径
*/
public static String getPhoneCardPath() {
return Environment.getDataDirectory().getPath();

}

/**
* 获取sd卡路径
*/
public static String getNormalSDCardPath() {
return Environment.getExternalStorageDirectory().getPath();
}

/**
* 获取sd(外置)卡路径
*/
public static String getSDCardPath() {
String cmd = "cat /proc/mounts";
Runtime run = Runtime.getRuntime();// 返回与当前 Java 应用程序相关的运行时对象
BufferedReader bufferedReader = null;
try {
Process p = run.exec(cmd);// 启动另一个进程来执行命令
bufferedReader = new BufferedReader(new InputStreamReader(new BufferedInputStream(p.getInputStream())));
String lineStr;
while ((lineStr = bufferedReader.readLine()) != null) {
Log.i(TAG, "proc/mounts: " + lineStr);
if (lineStr.contains("sdcard")
&& lineStr.contains(".android_secure")) {
String[] strArray = lineStr.split(" ");
if (strArray.length >= 5) {
String sdcard = strArray[1].replace("/.android_secure", "");
//return sdcard;
}
}
if (p.waitFor() != 0 && p.exitValue() == 1) {
// p.exitValue()==0表示正常结束,1:非正常结束
Log.e(TAG, cmd + " 命令执行失败");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return Environment.getExternalStorageDirectory().getPath();
}

/**
* 查看所有的sd路径
*/
public static String getSDCardPathEx() {
String mount = "";
try {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("mount");
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
String line;
BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
Log.i(TAG, "mount: " + line);
if (line.contains("secure")) {
continue;
}
if (line.contains("asec")) {
continue;
}

if (line.contains("fat")) {
String columns[] = line.split(" ");
if (columns.length > 1) {
mount = mount.concat("*" + columns[1] + "\n");
}
} else if (line.contains("fuse")) {
String columns[] = line.split(" ");
if (columns.length > 1) {
mount = mount.concat(columns[1] + "\n");
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return mount;
}

//获取当前路径,可用空间
public static long getAvailableSize(String path) {
try {
File base = new File(path);
StatFs stat = new StatFs(base.getPath());
return stat.getBlockSizeLong() * ((long) stat.getAvailableBlocksLong());
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}

/**
* 获取 SD card 信息
*/
Expand Down Expand Up @@ -72,15 +180,15 @@ public static class SDCardInfo {
@Override
public String toString() {
return "SDCardInfo{" +
"isExist=" + isExist +
", totalBlocks=" + totalBlocks +
", freeBlocks=" + freeBlocks +
", availableBlocks=" + availableBlocks +
", blockByteSize=" + blockByteSize +
", totalBytes=" + totalBytes +
", freeBytes=" + freeBytes +
", availableBytes=" + availableBytes +
'}';
"isExist=" + isExist +
", totalBlocks=" + totalBlocks +
", freeBlocks=" + freeBlocks +
", availableBlocks=" + availableBlocks +
", blockByteSize=" + blockByteSize +
", totalBytes=" + totalBytes +
", freeBytes=" + freeBytes +
", availableBytes=" + availableBytes +
'}';
}
}
}

0 comments on commit 77af9f4

Please sign in to comment.