多くの質問が回答されていますが、 ですが、私の場合、私のコードはOppo、samsungの電話では正しく機能していますが、MI、MOto G、Lenavo電話ここで生ファイルから外部(セカンダリ)SDカードへのファイルコピー
私のコードです:
二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;
}`
そして、私はパスを使用して、ファイルのコピーこのコード:(パス+ File.separator + "name.txt")
のOutputStream OS =新しいのFileOutputStream。
アンドロイドはオープンソースなので、あるデバイスで何かが動作していても、同じことは機能しません。私は何度も直面しています。 – karan
次に、この問題からどうやって解決できますか? –
/system/files/...のようなフォルダ構造をハードコードしないでください。http://stackoverflow.com/a/19465224/3172518 – karan