我的設備是HTC一個雙卡,由於某種原因Environment.getExternalStorageDirectory()是我手機的內存,這是不可拆卸的SD卡。 我試圖用這個來找到真正的SD卡路徑:找到真正的SD卡路徑和文件寫入
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;
}
和我 到/ mnt/media_rw/ext_sd
我試着寫文件到/ mnt/media_rw/ext_sd /下載 但該文件似乎沒有被創建。
File file = new File("/mnt/media_rw/ext_sd/downloads", "test.txt");
FileWriter writer = null;
try {
writer = new FileWriter(file);
writer.write("sdfsdfsfd");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
02-11 23:12:35.236 9110-9110/www.jenyakirmiza.com.testsdcard W/System.err﹕ java.io.FileNotFoundException: /mnt/media_rw/ext_sd/downloads/test.txt: open failed: EACCES (Permission denied)
我聽說從4.4開始限制spath,所以現在我們不能寫文件到可移動的SD卡。但他們說,你可以寫申請到/sdcardpath/Android/data/your.package.name
PS。當然,我添加了write_external權限來顯示。
您可能想了解更多關於[內部存儲](http://commonsware.com/blog/2014/04/07/storage-situation-internal-storage.html),[外部存儲]( http://commonsware.com/blog/2014/04/08/storage-situation-external-storage.html)和[可移動存儲](http://commonsware.com/blog/2014/04/09/storage -situation-removable-storage.html)意味着從Android SDK的角度來看。 – CommonsWare 2015-02-11 21:22:48
你的先生張貼在我的問題上的榮譽。我用了很多你創造的東西。非常感謝你對android社區的貢獻。我會讀你所建議的一切。 – 2015-02-11 22:59:01