使用DownloadManager将文件下载到真正的SD卡,而不是模拟存储

nol*_*nic 5 java eclipse android

我有一个星系S4,我的问题是我无法使用DownloadManager将文件下载到SD卡,在模拟存储上它工作正常,对于真正的SD卡它给了我这个错误:

java.lang.SecurityException: Destination must be on external storage: file:///mnt/extSdCard/Android/data/net.myApp/files/exercises/exec.mp4
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

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.
Run Code Online (Sandbox Code Playgroud)

如果我将目的地设置为:

File destination = new File(getExternalFilesDir(null), "exec.mp4");
Run Code Online (Sandbox Code Playgroud)

代码工作得很好,因为目标来到模拟存储.我也可以创建文件到SD卡,如:

File f = new File("/mnt/extSdCard/Android/data/net.myApp/files/exercises/exec.mp4");
f.createNewFile();
Run Code Online (Sandbox Code Playgroud)

它工作,但我不能使用该路径的下载管理器.

请帮忙.我需要能够将文件下载到该路径.

nol*_*nic 2

在花了一些时间解决这个问题后,我找到了一个更优化的解决方案,然后在下载后将文件复制到任意位置。主要问题是我设置的SD卡路径不正确。

它是:/mnt/extSdCard/Android/data...

但在三星 Galaxy S4 上,它应该是:/storage/extSdCard/Android/data...

默认下载管理器似乎可以很好地将此路径作为下载目的地。在以下链接中,我发现了一个非常好的代码,用于检测不同设备的 SD 卡路径:http ://www.javacodegeeks.com/2012/10/android-finding-sd-card-path.html

它看起来很奇怪,但对我来说,这似乎是最优的解决方案。