Anuvrat Chitranshi
Anuvrat Chitranshi

Reputation: 71

How to use Download Manager with Storage access framework / Mediastore

I need to download a pdf file and then fire an intent so that other suitable app can show the file. Till now, I was using setDestinationInExternalPublicDir of DownloadManager to set the destination of my download as a subdirectory of download directory. Then I used getExternalStoragePublicDirectory to create the file and get the uri through FileProvider and ultimately fired the intent with ACTION_VIEW.

Now with scoped storage, getExternalStoragePublicDirectory is deprecated. I was planning to use SAF/MediaStore but they give content uri which I cannot use to set destination for download in DownloadManager. Is there a way to resolve this? I was thinking to store the content uri that one can get through the DownloadManager.getUriForDownloadedFile in shared pref and then use it to fire intent without needing getExternalStoragePublicDirectory. But I want to avoid such 'hacks' if there is a compatible way of using DownloadManager within scoped storage compliance restrictions.

Upvotes: 3

Views: 876

Answers (1)

blackapps
blackapps

Reputation: 9282

Then I used getExternalStoragePublicDirectory to create the file and get the uri through FileProvider and ultimately fired the intent with ACTION_VIEW.

No. That is not the way to go.

Instead you should register a broadcast receiver for ACTION_DOWNLOAD_COMPLETE.

In onReceive() you can obtain an uri for the downloaded file.

Use that uri with a grant read permission flag for your intent ACTION_VIEW.

Upvotes: 0

Related Questions