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

标签 java android eclipse

我有一个 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/

相关文章:

java - 无法读取另一个可分割对象中的可分割对象

eclipse - 在eclipse中安装objectaid,没有找到有效的许可证

ios - 使用 tabris iOS 客户端更改服务器 URL?

Java程序使用简单循环查找整数数组的重复值

java - Java 中有哪些不同的 jar 文件,以及如何将它们导入到 Eclipse 中?

JavaFX + openjdk-10-jdk

java - 关于使用 Clojure 在 Eclipse 上开发 GAE 应用程序的问题

java - 如何一起使用 MaskFormatter 和 DocumentFilter

android - Cordova android 应用程序未在主屏幕上显示图标

java - Android 上的蓝牙延迟补偿