Download file insid...
 
Share:
Notifications
Clear all

[Solved] Download file inside WebView

  

0
Topic starter

I have a webview in my Android Application. When user goes to webview and click a link to download a file nothing happens. How to fix this. Please help me.

Topic Tags
2 Answers
3

in MainActivity.java

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, URLUtil.guessFileName(url, contentDisposition, mimetype));
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(),"Downloading File",
Toast.LENGTH_LONG).show();
}
});

And also Remember that you need set the write to external storage permission in the manifest. If not above code will not work.

in AndroidMainfest.xml

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

 

And also you must request user permission for devices.

Thanks it worked!

1

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