我正在尝试以编程方式将文件从 Android 的内部存储器 move 到 SD 卡中的现有目录。
我尝试了两种方法。
在第一个中我使用了 File.renameTo:
String destName = externalDirPath + File.separatorChar + destFileName;
File originFile = new File(cacheDirPath + File.separatorChar + originalfileName);
originFile.renameTo(new File(destName));
在另一个中我使用了 Runtime.getRuntime():
Process p = Runtime.getRuntime().exec("/system/bin/sh -");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
String command = "cp " + cacheDirPath + "/" + originalfileName+ " " + externalDirPath + "/" + destFileName+ "\n";
os.writeBytes(command);
两者都行不通..
有什么建议吗?
最佳答案
根据 renameTo
的 Android API 引用,
Both paths be on the same mount point. On Android, applications are most likely to hit this restriction when attempting to copy between internal storage and an SD card.
您可能需要将 File
读入 byte[]
,然后将其写入新的 File
。
This answer覆盖它。
关于android - 以编程方式将文件从缓存目录 move 到 SDCard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10656512/