2

I'm working on an android application which uses a webview to open my site (url: learnportal.me) and then, upon entering the correct credentials, it will load a normal html page which has multiple links to download (those are all pdf/ppt/doc files).
All of this happens inside the webview.

All I want is the downloads to happen directly within the application itself, without any redirection to the default web browser in the mobile device.

Further more, I need the files to be downloaded to the SD card.

This is the MainActivity Page I'm currently working on... not sure if its correct though

public class MainActivity extends Activity {  
 @SuppressWarnings("deprecation")  
 @SuppressLint("SetJavaScriptEnabled")  
 @Override  
 protected void onCreate(Bundle savedInstanceState) {  
      super.onCreate(savedInstanceState);  
      WebView webview = new WebView(this);  
      webview.setWebChromeClient(new WebChromeClient());  
      WebViewClient client = new ChildBrowserClient();  
      webview.setWebViewClient(client);  
      WebSettings settings = webview.getSettings();  
      settings.setJavaScriptEnabled(true);  
      //webview.setInitialScale(1);  
      //webview.getSettings().setUseWideViewPort(true);  
      settings.setJavaScriptCanOpenWindowsAutomatically(false);  
      //settings.setBuiltInZoomControls(true);  
      settings.setPluginState(PluginState.ON);  
      settings.setDomStorageEnabled(true);  
      webview.loadUrl("http://learnportal.me");  
      //webview.setId(5);  
      //webview.setInitialScale(0);  
      //webview.requestFocus();  
     // webview.requestFocusFromTouch();  
      setContentView(webview);  
 }  
 /**  
  * The webview client receives notifications about appView  
  */  
 public class ChildBrowserClient extends WebViewClient {  
      @SuppressLint({ "InlinedApi", "NewApi" })  
      @Override  
      public boolean shouldOverrideUrlLoading(WebView view, String url) {  
           boolean value = true;  
           String extension = MimeTypeMap.getFileExtensionFromUrl(url);  
           if (extension != null) {  
                MimeTypeMap mime = MimeTypeMap.getSingleton();  
                String mimeType = mime.getMimeTypeFromExtension(extension);  
                if (mimeType != null) {  
                     if (mimeType.toLowerCase().contains("pptx")  
                               || extension.toLowerCase().contains("pdf")  
                               || extension.toLowerCase().contains("doc")
                               || extension.toLowerCase().contains("ppt")){  
                          DownloadManager mdDownloadManager = (DownloadManager) MainActivity.this  
                                    .getSystemService(Context.DOWNLOAD_SERVICE);  
                          DownloadManager.Request request = new DownloadManager.Request(  
                                    Uri.parse(url));
                          File destinationFile = new File(Environment.getExternalStorageDirectory(),getFileName(url));
                          request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
                          request.setDescription("Downloading via LearnPortal..");  
                          request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                          request.setDestinationUri(Uri.fromFile(destinationFile));
                          mdDownloadManager.enqueue(request);  
                          value = false;  
                     }  
                }  
                if (value) {  
                     view.loadUrl(url);  
                }  
           }  
           return value;  
      }  
      @Override  
      public void onPageFinished(WebView view, String url) {  
           super.onPageFinished(view, url);  
      }  
      /**  
       * Notify the host application that a page has started loading.  
       *   
       * @param view  
       *      The webview initiating the callback.  
       * @param url  
       *      The url of the page.  
       */  
      @Override  
      public void onPageStarted(WebView view, String url, Bitmap favicon) {  
           super.onPageStarted(view, url, favicon);  
      }  
 }  
 /**  
  * File name from URL  
  *   
  * @param url  
  * @return  
  */  
 public String getFileName(String url) {  
     String filenameWithoutExtension = "";  
     filenameWithoutExtension = String.valueOf(System.currentTimeMillis());  
     return filenameWithoutExtension;  
}
}
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Hisham Haniffa
  • 449
  • 5
  • 7

1 Answers1

-1

http://developer.android.com/reference/android/webkit/DownloadListener.html

use DownloadListener and override onDownloadStart

for the actual download logic check onDownloadStartNoStream in the below link

https://android.googlesource.com/platform/packages/apps/Browser/+/android-4.4_r1.1/src/com/android/browser/DownloadHandler.java

vjprabu
  • 1
  • 2
  • sure i will give it a try.If anyone here can fix my above code to my requirement it will be a great help..i have being working on this for a weeks time trying to figure out proper solution – Hisham Haniffa Dec 28 '13 at 19:14