I\'ve got to fix our App for Android N due to the FileProvider changes. I\'ve basically read all about this topic for the last ours, but no solution found did work out for me.>
I had a similar issue and solved it by not automatically opening the file but showing the 'Download complete' notification and then let the Android system open the file when the user clicks on the notification.
To notify the user additionally, I'm showing a Toast when the download is completed.
DownloadManager mManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
String url = "your URL";
String filename = "file.pdf";
// Set up the request.
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url))
.setTitle("Test")
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDescription("Downloading...")
.setMimeType("application/pdf");
request.allowScanningByMediaScanner();
mManager.enqueue(request);
BroadcastReceiver:
public class DownloadReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
case DownloadManager.ACTION_DOWNLOAD_COMPLETE:
Toast.makeText(context, "Download completed", Toast.LENGTH_SHORT).show();
break;
}
}
}