80,361
社区成员
发帖
与我相关
我的任务
分享
public static List<String> getStorageList() throws IOException {
File file = new File("/proc/mounts");
if (file.canRead()) {
BufferedReader reader = null;
List<String> result = new ArrayList<String>();
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String lines;
while ((lines = reader.readLine()) != null) {
String[] parts = lines.split("\\s+");
if (parts.length >= 2) {
if (parts[0].contains("vold")) {
result.add(parts[1]);
}
}
}
return result;
}
return null;
}
如果这个代码还找不到的话,那我就真帮不了你了,因为正常来说linux的挂载情况都会记录到 /proc/mounts 里面,如果你手里的设备在SD卡已经挂载的情况下,仍然无法在 /proc/mounts 里面找到对应路径,那只能说你手里的设备所使用的linux与众不同,可能是被深度定制化过的,包括你说华为那个手机没有权限读取这个文件也可能是这个问题。这种情况下可能你需要通过写程序的方式,将你程序能访问到的文件系统导出来去找到这台设备对应的挂载信息文件了。
public static Map<String, File> getAllStorageLocations() {
Map<String, File> map = new HashMap<String, File>(10);
List<String> mMounts = new ArrayList<String>(10);
List<String> mVold = new ArrayList<String>(10);
mMounts.add("/mnt/sdcard");
mVold.add("/mnt/sdcard");
try {
File mountFile = new File("/proc/mounts");
if (mountFile.exists()) {
Scanner scanner = new Scanner(mountFile);
while (scanner.hasNext()) {
String line = scanner.nextLine();
if (line.startsWith("/dev/block/vold/")) {
String[] lineElements = line.split(" ");
String element = lineElements[1];
// don't add the default mount path
// it's already in the list.
if (!element.equals("/mnt/sdcard"))
mMounts.add(element);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
try {
File voldFile = new File("/system/etc/vold.fstab");
if (voldFile.exists()) {
Scanner scanner = new Scanner(voldFile);
while (scanner.hasNext()) {
String line = scanner.nextLine();
if (line.startsWith("dev_mount")) {
String[] lineElements = line.split(" ");
String element = lineElements[2];
if (element.contains(":"))
element = element.substring(0, element.indexOf(":"));
if (!element.equals("/mnt/sdcard"))
mVold.add(element);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < mMounts.size(); i++) {
String mount = mMounts.get(i);
if (!mVold.contains(mount))
mMounts.remove(i--);
}
mVold.clear();
List<String> mountHash = new ArrayList<String>(10);
for (String mount : mMounts) {
File root = new File(mount);
if (root.exists() && root.isDirectory() && root.canWrite()) {
File[] list = root.listFiles();
String hash = "[";
if (list != null) {
for (File f : list) {
hash += f.getName().hashCode() + ":" + f.length() + ", ";
}
}
hash += "]";
if (!mountHash.contains(hash)) {
String key = SD_CARD + "_" + map.size();
if (map.size() == 0) {
key = SD_CARD;
} else if (map.size() == 1) {
key = EXTERNAL_SD_CARD;
}
mountHash.add(hash);
map.put(key, root);
}
}
}
mMounts.clear();
if (map.isEmpty()) {
map.put(SD_CARD, Environment.getExternalStorageDirectory());
}
return map;
}
Environment.getExternalStorageDirectory().getAbsolutePath() 在华为荣耀手机上获得的路径是: /storage/emulated/0 实际SD卡路径是: 内置:storage/sdcard0 外置:storage/sdcard1 求解决方案,感觉这个问题应该不难,但是搜索不到合适的答案
String sdcard = Environment.getExternalStorageDirectory().toString();