文件未在WebView中下载

时间:2016-11-09 13:00:47

标签: android url webview download

我正在使用Android应用程序,我需要在webview中打开表单URL。我可以在webview中打开表单,有一个带表单的提交按钮,单击此按钮将在webview中下载文件,但它没有发生。我可以使用谷歌浏览器等移动设备上的普通浏览器下载文件。如何在我的应用程序webview中下载文件。

1 个答案:

答案 0 :(得分:0)

我不知道你是怎么回事,但是你可以使用这段代码在webview中下载文件

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

        }
    });

并将此权限添加到您的Android清单文件

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
希望它会有所帮助。!