繁体   English   中英

使用 Android 11 中的 DownloadManager 将文件下载到存储

[英]Downloading a file to storage with DownloadManager in Android 11

使用 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM