Tuesday, September 2, 2014

Downloading file from android webview over https (ssl)

If you are using android version lower than 4.1.2 ( API16), then downloading a file from android webview over https (ssl) might not work properly.

Please go through this post first : Downloading file from android webview , for instructions on how to download it over http.
Same instructions will be applicable for https including the additional instructions described in  below steps.

In android version lower than 4.1.2 , download manager doesn't handle https protocol. so you have to copy that class to your project and have to use it instead of that from sdk.

You should not use that custom download manager for all android targets. Because for newer versions that class already handles https. and some additional features might have been added to it.
So you can conditionally check for versions and use either of download manager.

if(android.os.Build.VERSION.SDK_INT<16) //for android 4.1.2 or lower
{
 com.test.android.app.DownloadManager downloadManager = new com.test.android.app.DownloadManager(getContentResolver(), "com.test"); // you have to provide your application package here
 com.test.android.app.DownloadManager.Request request = new com.test.android.app.DownloadManager.Request(Uri.parse(url));
 request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "app_download_directory");
 downloadManager.enqueue(request);
}
else //for newer version, use download manager from sdk
{
 Request request = new Request(Uri.parse(url));
 request.allowScanningByMediaScanner();
 request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "app_download_directory");
 DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
 dm.enqueue(request);
}

You have to add 2 SDK classes in your project : android.app.DownloadManager , android.provider.Downloads

You can download it from here : Download source files

Copy these 2 classes in your project and refer those in above code snippet. (replace com.test with your project’s package)



1 comment:

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...