Updating Android “Downloads” folder without using

2019-03-01 13:46发布

问题:

I'm currently running Marshmallow, and I have my application set up to download files into the phone's download folder by using the following path:

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)

and an AsyncTask. Everything works fine, I can access the files downloaded.

Though, the problem is that the files are only accessible on the phone using a File Browser, and not through the default Downloads folder via Android applications. I understand that you can do this automatically by using the Android DownloadManager, but I wanted to keep my current implementation of an AsyncTask without having to use DownloadManager.

My question is, is there a way I can simply "refresh" or "update" what is in the Downloads folder so they show up using the Android Downloads application?

I have also tried using the media scanner Intent (to refresh the gallery), but from my understanding, this does not work for Marshmellow.

activity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));

回答1:

is there a way I can simply "refresh" or "update" what is in the Downloads folder so they show up using the Android Downloads application?

The Downloads application only shows what is in its own ContentProvider, which in turn is driven by what was downloaded through DownloadManager. It is not showing the contents of any specific on-device directory, and that ContentProvider is not documented or supported for independent access.

I have also tried using the media scanner Intent (to refresh the gallery), but from my understanding, this does not work for Marshmellow.

That, or using MediaScannerConnection and scanFile(), will allow apps that query the MediaStore to see your file. This includes the MTP daemon; MTP is how a desktop OS views the contents of an Android device's external storage. None of that directly affects the Downloads app.



回答2:

I don't this this is possible without the DownloadManager class. I use the following in an AsyncTask

DownloadManager downloadManager = (DownloadManager)mainActivity.getSystemService(mainActivity.DOWNLOAD_SERVICE);
downloadManager.addCompletedDownload(file.getName(), file.getName(), true, "application/json", file.getAbsolutePath(),file.length(),true);

This will make the file appear in the Downloads app on 6.0, even if the file was created locally.