【发布时间】:2016-03-14 19:08:08
【问题描述】:
我正在尝试将位于外部存储目录中的文件复制到我的 SD 卡中的目录中。但是,当我检查文件是否已成功复制时,该文件甚至没有在 SD 卡中创建。
我错过了什么吗?这是我的代码:
String sourcePath = Environment.getExternalStorageDirectory().getPath() + newFileName;
File source = new File(Environment.getExternalStorageDirectory().getPath(), newFileName);
String destinationPath = "/storage/external_SD";
File destination = new File(destinationPath, newFileName);
try {
if(!destination.exists()){
destination.mkdir();
}
FileUtils.copyFile(source, destination);
} catch (IOException e) {
e.printStackTrace();
}
copyFile 方法来自 Apache 库。这是它的链接:https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html
【问题讨论】:
-
可以分享copyFile的代码吗?并执行 mkdirs() 这也将创建中间目录。
-
您在 Android 4.4+ 上没有对removable storage 的任意文件系统级访问权限。
-
@dex,我编辑添加了一个链接。另外,我确实做了一个 mkdir。看看我的代码。
-
@CommonsWare - 我们又见面了。有解决办法吗?谢谢
标签: java android android-sdcard android-file android-external-storage