简体   繁体   中英

Downloading a file to storage with DownloadManager in Android 11

With Android 11, file storage has become a bit of a mess to understand. I've been banging my head against a wall, looking through dozens of questions on SO, for the past day trying to download a simple mp4 to the device, but nothing's working.

Here's what I have:

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);

And here is my 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();
    }

}

I have no idea if it's downloading and I can't see any files in the shares directory that I've created. What am I doing wrong?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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