Filip
Filip

Reputation: 45

WebView & DownloadManager

I'm makin' an app, which job is to load one specific website. Everything works fine except downloading files.

@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 //whatever i use as agent it wont work

contentDisposition: attachment;filename=1449822583856.jpg

mimetype: image/jpeg

contentLength: -1

It downloads my homesite as phpfile, not my jpg. Any ideas? Thanks for helping :)

Edit: It might be problem with Content-Length? Or maybe some session problems?

Upvotes: 1

Views: 782

Answers (1)

Zain Ul Abidin
Zain Ul Abidin

Reputation: 2690

It is obvious that it will download a php file because the url you provided does not contain the direct address of file

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

so entering it in browser displays image but the address is not belongs to any file directly try to get that img element from webview that is being displayed in Webview and get its src attribute value pass that to DownloadManager and you will get your image downloaded

Upvotes: 1

Related Questions