簡體   English   中英

如何在webView中下載文件?

[英]How to download files in webView?

有兩種方法可以在 webView 中下載文件 -

1)通過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) 打開應用選擇器以通過 3rd 方應用下載-

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

但是我如何讓它們一起工作? 我希望我的應用程序顯示一個對話框,它為您提供兩個選項:在應用程序內下載或通過第 3 部分應用程序下載。

您可以使用此代碼段

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

這是我添加的片段,它將向您顯示兩個選項,例如您只想將邏輯放在按鈕的點擊方法中。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM