Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading pdf through web browser and webview

I am accessing a website which has .pdf documents. If I open that document through a web browser, it starts downloading it. If I open it through webview, nothing happens. What setting should I need to apply to webview to make it start downloading?

I already have this.

wvA.setDownloadListener(new DownloadListener()
{
    public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType,
                    long size)
    {
        Intent viewIntent = new Intent(Intent.ACTION_VIEW);
        viewIntent.setDataAndType(Uri.parse(url), mimeType);

        try
        {
            startActivity(viewIntent);
        }
        catch (ActivityNotFoundException ex)
        {
        }
    }
});
like image 560
user200658 Avatar asked Jan 07 '13 17:01

user200658


People also ask

Can WebView load PDF?

Opening a PDF file in Android using WebView All you need to do is just put WebView in your layout and load the desired URL by using the webView. loadUrl() function. Now, run the application on your mobile phone and the PDF will be displayed on the screen.

How do I save a WebView as a PDF?

Create a custom WebViewClient (reference) and set it on your WebView . In this WebViewClient you should override shouldOverrideUrlLoading (WebView view, String url) . From here on you can download the PDF manually when it is clicked. They asked to save the contents of the WebView as a pdf.


2 Answers

Implement a download listener to handle any kind of download:

webView.loadUrl(uriPath);

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

        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
    }
});
like image 83
Sileria Avatar answered Oct 06 '22 01:10

Sileria


You should create WebViewClient and set it to your webview. Every time you click a link WebViewClient's shouldOverrideUrlLoading method will be called. Check that url points to pdf file and do what you want. For example, you can view pdf.

webView.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading (WebView view, String url) {
        if (url.endsWith(".pdf")) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            // if want to download pdf manually create AsyncTask here
            // and download file
            return true;
        }
        return false;
    }
});
like image 39
Leonidos Avatar answered Oct 06 '22 01:10

Leonidos