如何在webView中下载文件? [英] How to download files in webView?

查看:247
本文介绍了如何在webView中下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两种方法可以在webView中下载文件-

There are two ways to download files in a webView-

1)通过网络视图下载

// 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)打开应用选择器以通过第三方应用程序下载-

 // 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.

推荐答案

您可以使用此代码段

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

这是我添加的代码段,它将向您显示两个选项,就像您只想将逻辑放在按钮的on-click方法中一样.

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

这篇关于如何在webView中下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆