Android webview下载数据/文本文件

时间:2016-05-19 03:31:28

标签: android android-webview android-download-manager

我开发了一个android webview并试图在点击webview中的链接时下载生成的数据:文本文件。

EditText

但是,我收到以下错误异常:

    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.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");
            DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            dm.enqueue(request);
        }
    });

这有什么解决方案吗?提前谢谢。

2 个答案:

答案 0 :(得分:0)

mWebView.setDownloadListener(new DownloadListener() {       

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); //Notify client once download is completed!
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Name of your downloadble file goes here, example: Mathematics II ");
            DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            dm.enqueue(request);
            Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); //This is important!
            intent.addCategory(Intent.CATEGORY_OPENABLE); //CATEGORY.OPENABLE
            intent.setType("*/*");//any application,any extension
            Toast.makeText(getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded
                    Toast.LENGTH_LONG).show();

        }
    });

不要忘记给予此许可!这是非常重要的!将其添加到您的清单文件(AndroidManifest.xml文件)

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

希望这会有所帮助。干杯:)

答案 1 :(得分:0)

要下载,请尝试:

wv.setDownloadListener(new DownloadListener() {
if(!url.contains("http://"){
    newurl=("http://"+url);
}
else
{
   newurl=url;
}
        public void onDownloadStart(String url, String userAgent,
                                    String contentDisposition, String mimetype,
                                    long contentLength) {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(newurl));
            startActivity(i);
        }
    });

onDownloadStart方法用于告诉系统下载完成后该怎么做。

另一种方法是编辑初始代码:

webView.setDownloadListener(new DownloadListener() {
String newurl;
    @Override
    public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {
    if(!url.contains("http://"){
        newurl=("http://"+url);
    }
    else
    {
       newurl=url;
    }
        DownloadManager.Request request = new DownloadManager.Request(
                Uri.parse(newurl));
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");
        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        dm.enqueue(request);
    }
});

不确定这是否会起作用