Android的智能手机各不同样。如今非常多Android智能手机都是自带存储,有的还带有扩展内存的Sdcard卡槽。
在Android开发中,非常多时候依据Android提供的获取SdCard存储方法获取不到不到存储文件夹的位置,因为Android的系统是linux系统,所以我想到使用linux命令mouunt来获取linux的挂载文件夹。
这样呢。基本上在Android上都能够获取到应用数据存放的文件夹。
使用命令获取到的文件夹我并没有遍历。假设你还是获取不到,能够把mount获去到的全部文件夹都遍历一次。
File sdcard ;
@SuppressLint("SdCardPath")
public File getSdCardFile() {
if (sdcard != null) {
return sdcard;
}
List<String> list = getExtSDCardPath();
boolean isRun = true;
if (list.size() > 0) {
sdcard = new File(list.get(list.size() - 1));
if (sdcard.isDirectory()) {
if (sdcard.getFreeSpace() == 0) {
isRun = true;
} else {
isRun = false;
}
}
}
if (isRun) {
sdcard = Environment.getExternalStorageDirectory();
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
sdcard = new File("/sdcard/");
log.e("sdcard spance:" + sdcard.getFreeSpace());
if (sdcard.getFreeSpace() == 0) {
sdcard = new File("/sdcard1/");
log.e("sdcard1 space:" + sdcard.getFreeSpace());
}
if (sdcard.getFreeSpace() == 0) {
sdcard = new File("/sdcard2/");
log.e("sdcard2 space:" + sdcard.getFreeSpace());
}
}
}
log.e("data:" + sdcard.getAbsolutePath());
return sdcard;
}
public List<String> getExtSDCardPath() {
List<String> lResult = new ArrayList<String>();
try {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("mount");
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
if (line.contains("extSdCard")) {
String[] arr = line.split(" ");
String path = arr[1];
if (path.lastIndexOf("extSdCard") == path.length() - 9) {
File file = new File(path);
if (file.isDirectory()) {
lResult.add(path);
}
}
} else if (line.contains("/sdcard")) {
String[] arr = line.split(" ");
String path = arr[1];
if (path.lastIndexOf("/sdcard") == path.length() - 6) {
File file = new File(path);
if (file.isDirectory()) {
lResult.add(path);
}
} else {
String number = path.substring(path.lastIndexOf("/sdcard") + 7);
try {
Integer.parseInt(number);
File file = new File(path);
if (file.isDirectory()) {
lResult.add(path);
}
} catch (Exception e) {
}
}
}
}
isr.close();
} catch (Exception e) {
}
return lResult;
}
只是,首选获取存储还是先使用Android提供的方法。推断并获取Sdcard文件夹。
//推断sdcard是否存在
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
//假设存在。获取存储File文件夹
Environment.getExternalStorageDirectory();
希望能够帮助到大家,对你有帮助或者认为值得借鉴的。能够在下方点个赞!谢谢。。!
如有转载请著名来自http://blog.csdn.net/qxs965266509