Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webview does not download file

Good day all, i have made an app but i have hit a wall. when you visit a website using a desktop browser like firefox or chrome and click on the download link it starts downloading the file, but when i open the same page in my android app that displays the page using webview and click on a download link nothing happens at all, it just sits there like it does not know what to do with that link. if you could help me out it would be a huge help and get me back on track again.

this is the download link i use for a pdf file on my site:

<img src="images/Art1.jpg" width="80" height="166" /><a href="m2.pdf">Download</a>

this is my main.xml file for my android app:

<?xml version="1.0" encoding="utf-8"?>
    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
    />

please i will be most gratefull if anyone can help point me on the right path.

package com.jeffonlinelibrary;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebPageLoader extends Activity
{
    final Activity activity = this;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.main);
        WebView webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);

        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress)
            {
                activity.setTitle("Loading...");
                activity.setProgress(progress * 100);

                if(progress == 100)
                    activity.setTitle(R.string.app_name);
            }
        });

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
            {
                // Handle the error
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }
        });

        webView.setDownloadListener(new DownloadListener() {

            public void onDownloadStart(String url, String userAgent,
                    String contentDisposition, String mimetype,
                    long contentLength) {
                // handle download, here we use brower to download, also you can try other approach.
                Uri uri = Uri.parse(url);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }
        });

        webView.loadUrl("http://jeffonlinelibrary.comuv.com/jeffOnlinelibraryApp");
    }
}
like image 505
chinedudeoracle Avatar asked Dec 10 '12 05:12

chinedudeoracle


People also ask

Can we download file in WebView?

Application Xamarin Forms Android. The application has a WebView element that displays different sites. On these sites, it is possible to download files. But the file cannot be downloaded via WebView.

Is Android WebView deprecated?

The Android system webview custom cache file has been deprecated and removed in Android 13. New apps and any app updates will now use the operating system default cache location.

Does WebView save cookies?

Cookie From API Service to WebViewThe WKWebsiteDataStore store cookies that can be accessed by all WKWebview .

What is WebView wrapper?

The WebView class is an extension of Android's View class that allows you to display web pages as a part of your activity layout. It does not include any features of a fully developed web browser, such as navigation controls or an address bar. All that WebView does, by default, is show a web page.


1 Answers

Set DownloadListener to your WebView. And use Brower app or DownloadManager to download the file.

    webview.setDownloadListener(new DownloadListener() {
        @Override
        public void onDownloadStart(String url, String userAgent,
                String contentDisposition, String mimetype,
                long contentLength) {
            // handle download, here we use brower to download, also you can try other approach.
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    });
like image 180
faylon Avatar answered Nov 06 '22 23:11

faylon