java使用DownloadManager将文件下载到真正的SD卡,而不是模拟存储
我有一个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();
它可以工作,但我不能用下载管理器的路径
请帮忙。 我需要能够将文件下载到该路径
# 1 楼答案
这是不受支持的。无法保证
DownloadManager
有任何权限写入任意路径,即使您认为它们指向SD卡。此外,您自己使用/mnt/extSdCard/
的代码可能在您的设备上工作,但不会在所有设备上工作,其中许多设备甚至没有这样的目录