WebView&下载管理器

时间:2015-12-28 10:19:50

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

我是makin'一个应用程序,哪个工作是加载一个特定的网站。除了下载文件外,一切正常。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    adviser = (WebView) findViewById(R.id.webView);
    getActionBar().hide();
    adviser.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);

            adviser.setDownloadListener(new DownloadListener() {
                public void onDownloadStart(String url, String userAgent,
                        String contentDisposition, String mimetype,
                        long contentLength) {

                    Request request = new Request(Uri.parse(url));

                    request.allowScanningByMediaScanner();
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                    request.setDestinationInExternalPublicDir(
                            Environment.DIRECTORY_DOWNLOADS,
                            customizeurl(contentDisposition));
                    DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                    dm.enqueue(request);

                    Toast.makeText(getApplicationContext(), "downloading",
                            Toast.LENGTH_LONG).show();
                }
            });
            return true;
        }
    });

    adviser.getSettings().setJavaScriptEnabled(true);
    adviser.getSettings().setSupportZoom(true);
    adviser.getSettings().setAllowFileAccess(true);
    adviser.getSettings().setAllowUniversalAccessFromFileURLs(true);

    adviser.getSettings().setUseWideViewPort(true);
    adviser.getSettings().setLoadWithOverviewMode(true);
    adviser.setVerticalScrollBarEnabled(true);
    adviser.setHorizontalScrollBarEnabled(false);
    adviser.getSettings().setAppCacheEnabled(true);
    adviser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    adviser.getSettings().setSupportMultipleWindows(true);

    adviser.getSettings().setUserAgentString("Mozilla");
    adviser.loadUrl("http://localhost/www/site/login");
}

url:localhost / www / site / download?action = newaction& dl = folder \ subfolder \ subsubfolder \ 1449822583856.jpg

uri:localhost / www / site / download?action = newaction& dl = folder \ subfolder \ subsubfolder \ 1449822583856.jpg

userAgent:Mozilla //无论我使用什么作为代理,它都无法正常工作

contentDisposition:attachment; filename = 1449822583856.jpg

mimetype:image / jpeg

contentLength:-1

它下载我的homesite作为phpfile,而不是我的jpg。 有任何想法吗? 感谢您的帮助:)

编辑:内容长度可能有问题?或者也许是一些会话问题?

1 个答案:

答案 0 :(得分:1)

很明显它会下载一个php文件,因为你提供的url不包含文件的直接地址

网址= localhost / www / site / download?action = newaction& dl = folder \ subfolder \ subsubfolder \ 1449822583856.jpg

所以在浏览器中输入它会显示图像,但是地址不属于任何文件直接尝试从Webview中显示的webview中获取img元素并获取其src属性值将其传递给DownloadManager,您将获得图像下载