简体   繁体   English

webview不下载文件

[英]webview does not download file

Good day all, i have made an app but i have hit a wall. 美好的一天,我已经做了一个应用程序,但我已经撞墙了。 when you visit a website using a desktop browser like firefox or chrome and click on the download link it starts downloading the file, but when i open the same page in my android app that displays the page using webview and click on a download link nothing happens at all, it just sits there like it does not know what to do with that link. 当您使用桌面浏览器(如firefox或chrome)访问网站并点击下载链接时,它会开始下载文件,但是当我在我的Android应用程序中打开同一页面时,使用webview显示页面并单击下载链接时没有任何反应它只是坐在那里,因为它不知道如何处理该链接。 if you could help me out it would be a huge help and get me back on track again. 如果你可以帮助我,那将是一个巨大的帮助,让我再次回到正轨。

this is the download link i use for a pdf file on my site: 这是我用于我网站上的pdf文件的下载链接:

<img src="images/Art1.jpg" width="80" height="166" /><a href="m2.pdf">Download</a>

this is my main.xml file for my android app: 这是我的Android应用程序的main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
    />

please i will be most gratefull if anyone can help point me on the right path. 如果有人能帮我指出正确的道路,我将非常感激。

package com.jeffonlinelibrary;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebPageLoader extends Activity
{
    final Activity activity = this;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.main);
        WebView webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);

        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress)
            {
                activity.setTitle("Loading...");
                activity.setProgress(progress * 100);

                if(progress == 100)
                    activity.setTitle(R.string.app_name);
            }
        });

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
            {
                // Handle the error
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }
        });

        webView.setDownloadListener(new DownloadListener() {

            public void onDownloadStart(String url, String userAgent,
                    String contentDisposition, String mimetype,
                    long contentLength) {
                // handle download, here we use brower to download, also you can try other approach.
                Uri uri = Uri.parse(url);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }
        });

        webView.loadUrl("http://jeffonlinelibrary.comuv.com/jeffOnlinelibraryApp");
    }
}

Set DownloadListener to your WebView. 将DownloadListener设置为WebView。 And use Brower app or DownloadManager to download the file. 并使用Brower app或DownloadManager下载文件。

    webview.setDownloadListener(new DownloadListener() {
        @Override
        public void onDownloadStart(String url, String userAgent,
                String contentDisposition, String mimetype,
                long contentLength) {
            // handle download, here we use brower to download, also you can try other approach.
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM