首先,通过反射机制获取swap开关的值:
/**
* 通过反射机制获取swap开关的值
*
* @return
*/
public static String getSwap() {
Object result = null;
try {
Class cl = Class.forName("android.os.SystemProperties");
Object invoker = cl.newInstance();
Method m = cl.getMethod("get", new Class[] { String.class, String.class });
result = m.invoke(invoker, new Object[] { "ro.mtk_2sdcard_swap", "unknown" });
return (String) result;
} catch (Exception e) {
e.printStackTrace();
}
return (String) result;
}
然后获取手机内置存储卡路径:
/**
* 获取手机内置存储卡路径
* @return
*/
public static String getInnerSDPath(){
List<String> list = SDCardScanner.getExtSDCardPaths();
String innerSDPath = "/storage/sdcard0";
if (list.size() == 0) {// 内置内存、外置内存不可用
innerSDPath = "/storage/sdcard0";
} else if (list.size() == 1) {// 没有外置内存或者外置内存不可用,内置内存可用
innerSDPath = "/storage/sdcard0";//list.get(0);
} else if (list.size() == 2) {// 内外置都可用
if ("1".equals(getSwap())) {// 开关打开,表示可以切换
innerSDPath = "/storage/sdcard1";//list.get(1);
}
else if ("0".equals(getSwap())) {// 开关关闭,表示不可以切换
innerSDPath = "/storage/sdcard0";//list.get(0);
}
}
return innerSDPath;
}
其中SDCardScanner类如下定义:
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import android.os.Environment;
/**
* 手机存储卡浏览类
* @author weizhi
* @version 1.0
*/
public class SDCardScanner {
/*
* avoid initializations of tool classes
*/
private SDCardScanner() {
}
/**
* @Title: getExtSDCardPaths
* @Description: to obtain storage paths, the first path is theoretically
* the returned value of
* Environment.getExternalStorageDirectory(), namely the
* primary external storage. It can be the storage of internal
* device, or that of external sdcard. If paths.size() >1,
* basically, the current device contains two type of storage:
* one is the storage of the device itself, one is that of
* external sdcard. Additionally, the paths is directory.
* @return List<String>
* @throws IOException
*/
public static List<String> getExtSDCardPaths() {
List<String> paths = new ArrayList<String>();
String extFileStatus = Environment.getExternalStorageState();
File extFile = Environment.getExternalStorageDirectory();
if (extFileStatus.equals(Environment.MEDIA_MOUNTED)
&& extFile.exists()
&& extFile.isDirectory()
&& extFile.canWrite()) {
paths.add(extFile.getAbsolutePath());
}
try {
// obtain executed result of command line code of 'mount', to judge
// whether tfCard exists by the result
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("mount");// 执行指定字符串命令
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
int mountPathIndex = 1;
while ((line = br.readLine()) != null) {
// format of sdcard file system: vfat/fuse
if ((!line.contains("fat") && !line.contains("fuse") && !line
.contains("storage"))
|| line.contains("secure")
|| line.contains("asec")
|| line.contains("firmware")
|| line.contains("shell")
|| line.contains("obb")
|| line.contains("legacy") || line.contains("data")) {
continue;
}
String[] parts = line.split(" ");
int length = parts.length;
if (mountPathIndex >= length) {
continue;
}
String mountPath = parts[mountPathIndex];
if (!mountPath.contains("/") || mountPath.contains("data")
|| mountPath.contains("Data")) {
continue;
}
File mountRoot = new File(mountPath);
if (!mountRoot.exists() || !mountRoot.isDirectory()
|| !mountRoot.canWrite()) {
continue;
}
boolean equalsToPrimarySD = mountPath.equals(extFile
.getAbsolutePath());
if (equalsToPrimarySD) {
continue;
}
paths.add(mountPath);
}
} catch (IOException e) {
e.printStackTrace();
}
return paths;
}
}