【发布时间】:2016-06-27 05:24:49
【问题描述】:
我知道已经回答了很多问题, 但就我而言,我的代码在 Oppo、三星手机上运行正常,但在 MI、MOto G、Lenavo 手机上运行不正常
这是我的代码:
这个String从SD卡的返回路径:
public static String getExternalStorage() {
File rootFolder = new File("/");
boolean isSdcardRemovable = false;
String path = null;
/* loop: */
for (int i = 0; i < rootFolder.listFiles().length; i++) {
if (rootFolder.listFiles()[i].listFiles() != null
&& !rootFolder.listFiles()[i].toString().contains("system")
&& !rootFolder.listFiles()[i].toString().contains("etc")
&& !rootFolder.listFiles()[i].toString().contains("dev")) {
File dataDir = new File(Environment.getDataDirectory()
.getAbsolutePath());
long dataDirSize = dataDir.getFreeSpace() / (1000 * 1000);
long folderSize = rootFolder.listFiles()[i].getFreeSpace()
/ (1000 * 1000);
if (dataDirSize == folderSize
|| (dataDirSize > folderSize && folderSize > (dataDirSize - 80))) {
System.err
.println("INTERNAL1 " + rootFolder.listFiles()[i]);
System.err.println(dataDirSize);
System.err.println(folderSize);
} else {
File rootSubFolder1 = new File(
rootFolder.listFiles()[i].getAbsolutePath());
if (rootSubFolder1.listFiles() != null) {
for (int j = 0; j < rootSubFolder1.listFiles().length; j++) {
if (rootSubFolder1.listFiles()[j].getTotalSpace() != 0
&& rootSubFolder1.listFiles()[j]
.getFreeSpace() != 0
&& rootSubFolder1.listFiles()[j]
.listFiles() != null) {
Debug.i("fromGetExternalStorage", ""
+ rootSubFolder1.listFiles()[j]);
if (rootSubFolder1.listFiles()[j].toString()
.contains("sdcard")
|| rootSubFolder1.listFiles()[j]
.toString().contains("storage")
|| rootSubFolder1.listFiles()[j]
.toString().contains("mnt")) {
folderSize = rootSubFolder1.listFiles()[j]
.getFreeSpace() / (1000 * 1000);
if (dataDirSize == folderSize
|| (dataDirSize > folderSize && folderSize > (dataDirSize - 80))) {
System.err
.println("INTERNAL2 "
+ rootSubFolder1
.listFiles()[j]);
System.err.println(dataDirSize);
System.err.println(folderSize);
} else {
int pos = rootSubFolder1.listFiles()[j]
.getAbsolutePath().lastIndexOf(
'/');
String str = rootSubFolder1.listFiles()[j]
.getAbsolutePath().substring(
pos + 1);
if (str.matches("(sd|ext|3039|m_external_sd).*")) {
isSdcardRemovable = true;
System.err.println("EXTERNAL "
+ rootSubFolder1
.listFiles()[j]);
System.err.println(dataDirSize);
System.err.println(folderSize);
path = rootSubFolder1.listFiles()[j]
.getAbsolutePath() + "/";
break loop;
}
}
}
}
}
}
}
}
}
if (isSdcardRemovable) {
if (path != null) {
Debug.i("new Path from getExternal Storage", path);
} else {
Debug.i("fail", "External memory not found.");
}
} else {
Debug.i("fail", "External memory not available.");
}
return path;
}`
我使用路径和复制文件的这段代码:
OutputStream OS = new FileOutputStream(path + File.separator + "name.txt");
【问题讨论】:
-
由于 android 是开源的,因此某些东西在一个设备上可以工作,而同样的东西却不能工作。我遇到过很多次。
-
那么,你是怎么解决这个问题的?
-
不要硬编码文件夹结构,如 /system/files/... 并尝试stackoverflow.com/a/19465224/3172518
-
我试了一下,只是放了/sdcard1/name.txt,但没有任何改变