Tuesday, September 2, 2014

Downloading file from android webview

Here we will see how to download a file from android webview over http.
If you are downloading over https (ssl) then refer to this tutorial : Downloading file from android webview over https (ssl)

You might have used webview in your android application. but if your webview’s html has download link then it might not work directly.

If you are downloading over http protocol then continue reading this post. But if you are downloading over https (ssl) then you may continue reading this but you will also need some additional code to be added for that. Please refer this post for more information after reading current post : Downloading file from android webview over https (ssl).

There are 2 ways to download the file from within android webview.

1) Downloading a file by directly using android download manager.
2) Downloading a file by using a web browser opened from android webview. Browser will take care of downloading a file. It will internally use download manager.

When you download a file using a download manager, a notification will appear in the notification bar and you can check the progress there as below.

downloading-file-from-webview.png

We will be creating a simple android app with only one activity. This will have only webview inside it.  
Webview will load html which will have download link. and on click of which we will download a target file using download manager and web browser as well.

First of all following properties will be needed in android manifest file.

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

WRITE_EXTERNAL_STORAGE is required if you have to download the file to sdcard.
If you are going to test the sample app using android emulator, then you have to enable sdcard on it, as shown in below screenshot of virtual device properties from AVD manager.

android-emulator-sdcard-setting.png

Below is the activity’s layout xml.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <WebView        
        android:id="@+id/webView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>

1) Downloading a file directly using android download manager

Following code will set the download listener for webview and will directly use download manager to download a file.


webView.setDownloadListener(new DownloadListener()
  {
   public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength)
   {
    //for downloading directly through download manager
    Request request = new Request(Uri.parse(url));
    request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");
    DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    dm.enqueue(request);
   }
  });

In the above code, if you don’t have sdcard on emulator/device, then you can use internal storage to store a file, by commenting this line.

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");

2) Downloading a file using a web browser opened from android webview

This will send an intent to web browser and web browser will take care of downloading the file. It will internally use download manager to download a file.

webView.setDownloadListener(new DownloadListener()
  {
   public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength)
   {    
    //download file using web browser
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);
   }
  });

If you are downloading over https (ssl) then refer to this tutorial : Downloading file from android webview over https (ssl)



3 comments:

  1. Enter your comment...

    ReplyDelete
  2. file not download. Application Keep Stopping (Open App Again).
    How to resolve it.

    ReplyDelete
  3. Your blog is too much amazing. I have found with ease what I was looking. Moreover, the content quality is awesome. Thanks for the nudge! www.vanced.pro/

    ReplyDelete

Creating and Deploying Java Web Application on AWS using Elastic Beanstalk

This tutorial is for creating simple java web application using eclipse and then deploying it on AWS cloud. Video tutorial for creating/de...