Open downloaded file on Android N using FileProvider

前端 未结 5 819
滥情空心
滥情空心 2021-02-05 17:11

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.

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-05 17:50

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

提交回复
热议问题