我有一个星系S4,我的问题是我无法使用DownloadManager将文件下载到SD卡,在模拟存储上它工作正常,对于真正的SD卡它给了我这个错误: 这是我的代码: 如果我将目的地设置为: 代码工作得很好,因为目标来到模拟存储。 我也可以创建文件到SD卡,如: 它 ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
使用 Android 11,文件存储变得有点难以理解。 在过去的一天里,我一直在用头撞墙,查看几十个关于 SO 的问题,试图将一个简单的 mp4 下载到设备上,但没有任何效果。
这是我所拥有的:
File file = new File(StorageUtils.getSharePath(context), "1.mp4");
Uri uri = Uri.parse(url);
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
request.setTitle("Downloading a file");
request.setVisibleInDownloadsUi(true);
request.setDestinationUri(Uri.fromFile(file));
videoDownloadId = downloadManager.enqueue(request);
这是我的StorageUtils
class:
public class StorageUtils {
public static String getAbsolutePath(Context context) {
return context.getExternalFilesDir(null).getAbsolutePath();
}
public static String getSharePath(Context context) {
String path = getAbsolutePath(context);
File dir = new File(path, "shares");
if (!dir.exists()) dir.mkdirs();
return dir.getAbsolutePath();
}
}
我不知道它是否正在下载,并且在我创建的shares
目录中看不到任何文件。 我究竟做错了什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.