[英]how to get secondary storage uri or root directory of secondary storage of all devices in android
[英]Get the public/root directory of a secondary external storage device in Android?
在 android 中,我可以使用以下方法獲取手機的可移動外部存儲:
for (File f : context.getExternalFilesDirs("/"))
if (Environment.isExternalStorageRemovable(f))
Log.println(Log.DEBUG, "#", f.getAbsolutePath());
但是,這將返回/storage/8E6A-06FF/Android/data/test.application/files
這不是我想要的,因為我只想要可移動的根路徑/storage/8E6A-06FF/
。 如何獲取手機可移動存儲的根路徑?
你可以試試這個,它對我來說很完美。它在所有操作系統的版本中都很有效。到目前為止,我沒有發現這個功能有任何問題。
public static String getSDPath() {
String filepath = "";
String[] strPath = {"/storage/sdcard1", "/storage/extsdcard",
"/storage/sdcard0/external_sdcard", "/mnt/extsdcard",
"/mnt/sdcard/external_sd", "/mnt/external_sd",
"/mnt/media_rw/sdcard1", "/removable/microsd", "/mnt/emmc",
"/storage/external_SD", "/storage/ext_sd",
"/storage/removable/sdcard1", "/data/sdext", "/data/sdext2",
"/data/sdext3", "/data/sdext4", "/emmc", "/sdcard/sd",
"/mnt/sdcard/bpemmctest", "/mnt/sdcard/_ExternalSD",
"/mnt/sdcard-ext", "/mnt/Removable/MicroSD",
"/Removable/MicroSD", "/mnt/external1", "/mnt/extSdCard",
"/mnt/extsd", "/mnt/usb_storage", "/mnt/extSdCard",
"/mnt/UsbDriveA", "/mnt/UsbDriveB"};
for (String value : strPath) {
File f = null;
f = new File(value);
if (f.exists() && f.isDirectory()) {
filepath = value;
break;
}
}
return filepath;
}
試試這個:
for (File f : context.getExternalFilesDirs("/"))
if (Environment.isExternalStorageRemovable(f))
Log.println(Log.DEBUG, "#", f.getParentFile().getParentFile().getParentFile().getParent());
context.getExternalFilesDirs() 將始終返回特定於應用程序的目錄。 但好消息是,特定於應用程序的目錄始終距存儲設備的根文件夾深 4 級。 因此,在 File f 上調用 getParentFile() 四次而不是 f.getAbsolutePath() 將使您獲得手機可移動存儲的根路徑。
也許只是在Android
拆分它?
我測試了它,它在我 請求許可后工作- WRITE_EXTERNAL_STORAGE
。
fun getBaseDir(dir: File): String {
val absPath = dir.absolutePath
return if (absPath.contains("/Android")) {
absPath.split("/Android")[0]
} else {
absPath
}
}
這將遍歷 sdcard 根目錄中的文件。 如果您想要主存儲,只需將 [1] 更改為 [0]。 getExternalFilesDirs返回主存儲和輔助存儲上應用程序目錄的路徑。 通過“Android”拆分第二個路徑后,第一個字符串將包含您的輔助存儲根目錄的路徑。 例如,在我的情況下,它是“/storage/B242-37B2/”。 使用minSdkVersion 19+ 。
String sdCardRoot = ContextCompat.getExternalFilesDirs(getApplicationContext(), null)[1].getAbsolutePath().split("Android")[0];
File f = new File(sdCardRoot);
File[] files = f.listFiles();
for (File inFile : files){
Log.d("Files", inFile.getName());
}
如果它對你有幫助,試試這個。 有關更多信息,請參閱此鏈接。
public static HashSet<String> getExternalMounts() {
final HashSet<String> out = new HashSet<String>();
String reg = "(?i).*vold.*(vfat|ntfs|exfat|fat32|ext3|ext4).*rw.*";
String s = "";
try {
final Process process = new ProcessBuilder().command("mount")
.redirectErrorStream(true).start();
process.waitFor();
final InputStream is = process.getInputStream();
final byte[] buffer = new byte[1024];
while (is.read(buffer) != -1) {
s = s + new String(buffer);
}
is.close();
} catch (final Exception e) {
e.printStackTrace();
}
// parse output
final String[] lines = s.split("\n");
for (String line : lines) {
if (!line.toLowerCase(Locale.US).contains("asec")) {
if (line.matches(reg)) {
String[] parts = line.split(" ");
for (String part : parts) {
if (part.startsWith("/"))
if (!part.toLowerCase(Locale.US).contains("vold"))
out.add(part);
}
}
}
}
return out;
}
這是另一種方法。 來源從這里。
Environment.getExternalStorageState() 返回內部 SD 掛載點的路徑,如“/mnt/sdcard”
但問題是關於外部 SD。 如何獲得像“/mnt/sdcard/external_sd”這樣的路徑(它可能因設備而異)?
如上所述,除了外部存儲之外,Android 沒有“外部 SD”的概念。
如果設備制造商選擇將外部存儲作為板載閃存並且也有 SD 卡,您將需要聯系該制造商以確定您是否可以使用 SD 卡(不保證)以及規則是什么使用它,例如使用什么路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.