Cassie Cassie - 4 months ago 148
Android Question

How to download files in webView?

There are two ways to download files in a webView-

1) download via webview

// download manager
webView.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.setMimeType(mimeType);
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
request.addRequestHeader("User-Agent", userAgent);
request.setDescription("Downloading file...");
request.setTitle(URLUtil.guessFileName(url, contentDisposition,
mimeType));
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();
}
});
// download manager


2) open app chooser to download via 3rd party apps-

// download via...
webView.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);
Toast.makeText(getApplicationContext(), "don't choose our app as it can't handle download intents, i have posted a question on stackoverflow though.",
Toast.LENGTH_SHORT).show();
}
});
// download via..


But how do i make both of them work together? I want my app to show a dialog which gives you two options of either downloading within the app or via 3rd part app.

Answer

You can use this snippet

         AlertDialog.Builder builder=new AlertDialog.Builder(this);
         builder.setMessage("How you want to download this file?")
        .setCancelable(false)
        .setPositiveButton("Use webView",new DialogInterface.OnClickListener(){
         public void onClick(DialogInterface dialog,int id){

        //your code here for download manager
        webView.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.setMimeType(mimeType);
        String cookies=CookieManager.getInstance().getCookie(url);
        request.addRequestHeader("cookie",cookies);
        request.addRequestHeader("User-Agent",userAgent);
        request.setDescription("Downloading file...");
        request.setTitle(URLUtil.guessFileName(url,contentDisposition,
        mimeType));
        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();} });
        dismiss();}})
        .setNegativeButton("3rd Party App",new DialogInterface.OnClickListener(){
         public void onClick(DialogInterface dialog,int id){

        // download via...
        webView.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);
        Toast.makeText(getApplicationContext(),"don't choose our app as it can't handle download intents, i have posted a question on stackoverflow though.",Toast.LENGTH_SHORT).show(); }});dialog.cancel();} });
        AlertDialog alert=builder.create();
        alert.show();

This is snippet i have added that will show you two options like you want just put your logic in button's on click method.

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("How you want to download?")
   .setCancelable(false)
   .setPositiveButton("By web", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            //use logic of downloading with web view here
            dialog.cancel();
       }
   })
   .setNegativeButton("Use third party tool", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            //use logic of third party tool here
            dialog.cancel();
       }
   });
AlertDialog alert = builder.create();
alert.show();
Comments