Processing WebView File Upload and Runtime Permissions

When I click the button in WebView , it should load the PDF file which will be saved in external storage.

To do this, I install DownloadListener :

 webview.setDownloadListener(new DownloadHandler()); public class DownloadHandler implements DownloadListener { @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { DownloadManager.Request request = new DownloadManager.Request( Uri.parse(url)); request.allowScanningByMediaScanner(); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, getFileName(url)); DownloadManager dm = (DownloadManager)getContext().getSystemService(getContext().DOWNLOAD_SERVICE); dm.enqueue(request); } } 

The problem occurs when trying to do this on Android M, where all permissions are set at runtime.

How do I first request permission before starting the download and continue or not with the download, depending on whether they were provided?

The download shouldInterceptRequest calls shouldInterceptRequest not shouldOverrideUrlLoading , so I don’t even know how I can cancel the request to check permissions first.

Requesting permissions after starting WebView not an option, because you can do much more than just upload files.

+6
source share

Source: https://habr.com/ru/post/1013402/


All Articles