我有一个 galaxy S4,我的问题是我无法使用 DownloadManager 将文件下载到 SD 卡,在模拟存储上它工作得很好,至于真正的 SD 卡,它给我这个错误:
java.lang.SecurityException: Destination must be on external storage: file:///mnt/extSdCard/Android/data/net.myApp/files/exercises/exec.mp4
这是我的代码:
DownloadManager dwlManager=(DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request dwlmRequest=new DownloadManager.Request(Uri.parse("http://cdn..."));
File destination = new File("/mnt/extSdCard/Android/data/net.myApp/files/exercises/exec.mp4");
dwlmRequest.setDestinationUri(Uri.fromFile(destination));
dwlManager.enqueue(dwlmRequest); //<--- Here comes the error.
如果我将目的地设置为:
File destination = new File(getExternalFilesDir(null), "exec.mp4");
代码工作得很好,因为目的地是模拟存储。 我也可以创建文件到 SD 卡,如:
File f = new File("/mnt/extSdCard/Android/data/net.myApp/files/exercises/exec.mp4");
f.createNewFile();
它可以工作,但我无法通过该路径使用下载管理器。
请帮忙。 我需要能够将文件下载到该路径。
最佳答案
这是不支持的。无法保证 DownloadManager
有权写入任意路径,即使您认为它们指的是 SD 卡。此外,您自己使用 /mnt/extSdCard/
的代码可能适用于您的设备,但并非适用于所有设备,其中许多设备甚至没有这样的目录。
关于java - 使用 DownloadManager 将文件下载到真正的 SD 卡,而不是模拟存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21116898/