Download File Inside Webview

Download file inside WebView

Have you tried?

mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});

Example Link: Webview File Download - Thanks @c49

Download file in Webview

Here a test with your link (in Kotlin). If you need a Java example, please, let me know:

private fun test() {
webView = findViewById(R.id.webView)

webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
Log.d(TAG, "shouldOverrideUrlLoading:url = ${url}")
if (url.contains("=download")){
Log.d(TAG, "shouldOverrideUrlLoading: ")
downloadFile(url)
webView.stopLoading()
}
return true
}
}
val url = "https://drive.google.com/uc?export=download&id=1rcPnhg9_rSes92BiQPotVjXuEAfFnyrf"
webView.loadUrl(url)
}

fun downloadFile(url: String) {
Log.d(TAG, "downloadFile: url = $url")
val manager = getSystemService(Activity.DOWNLOAD_SERVICE) as DownloadManager
val uri =
Uri.parse(url)
val request = DownloadManager.Request(uri)
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
val reference: Long = manager.enqueue(request)
}

image abuot download

Java code:

    private void test() {

webView = findViewById(R.id.webView);

webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("=download")){
downloadFile(url);
}

return super.shouldOverrideUrlLoading(view, url);
}
});

String url = "https://drive.google.com/uc?export=download&id=1rcPnhg9_rSes92BiQPotVjXuEAfFnyrf";
webView.loadUrl(url);
}

private void downloadFile(String url) {
DownloadManager manager = (DownloadManager) getSystemService(Activity.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
manager.enqueue(request);
}

How to Download a file in App with webview component?

As I said, if you want to download the file inside your webview, you should try this:

myWebView.setDownloadListener(new 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, "name of your file");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
//you could show a message here

}
});

Don't forget to add this permission on your manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

And if you are using Android > 5 , you have to ask for permissions :)

Link: https://developer.android.com/training/permissions/requesting.html



Related Topics



Leave a reply



Submit