0

Iam working on webview for loading this Url, my code loads the url fine but whenever i clicked on a file in that url it does't downloading, i think the problem is with file extensions.

And i also need the files to be downloaded to a particular folder in sdcard.

at present iam using the bellow code i dont't understand what could be the wrong in my code

my Main Activity is

public class GosActs extends Activity{

WebView web;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gos_acts);

    web = (WebView)findViewById(R.id.wGAweb);

    web.setWebViewClient(new WebViewClient(){

        public void onLoadResource(WebView view, String url) {

            //load.setVisibility(View.VISIBLE);
        }

        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            if(url.endsWith(".pdf")){
                Uri source = Uri.parse(url);
                DownloadManager.Request request = new DownloadManager.Request(source);
                request.setDescription("requested GO downloading");
                request.setTitle("go.pdf");

                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION);
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Go.pdf");

                DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                manager.enqueue(request);
            } else if(url.endsWith(".docx")){
                Uri source = Uri.parse(url);
                DownloadManager.Request request = new DownloadManager.Request(source);
                request.setDescription("requested GO downloading");
                request.setTitle("go.pdf");

                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION);
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Go.pdf");

                DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                manager.enqueue(request);
            }
            else
            view.loadUrl(url);

            return true;
        }

        public void onPageFinished(WebView view, String url) {
            //load.setVisibility(View.GONE);
        }

    });
    web.getSettings().setJavaScriptEnabled(true);
    web.getSettings().setBuiltInZoomControls(true);
    web.getSettings().setDisplayZoomControls(false);
    web.getSettings().setLoadWithOverviewMode(true);
    web.getSettings().setUseWideViewPort(true);
    web.loadUrl("http://goir.ap.gov.in");


}

my gos_acts.xml is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#288A0E"
android:orientation="vertical" >


<WebView
    android:id="@+id/wGAweb"
    android:layout_margin="5dip"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background_card" />   

</LinearLayout>

and i also add required permissions in my minifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/>
<uses-permission android:name="android.permission.INTERNET"/>
Vamsi Smart
  • 928
  • 9
  • 16
  • When you stepped through your code in a debugger, or added logging statements, is your code getting into `shouldOverrideUrlLoading()` and branching as you would expect? If yes, and you're sure that your `DownloadManager` code is being called, are there any messages in LogCat from the `Downloads` app? – CommonsWare May 26 '15 at 17:55
  • @CommonsWare i just chicked it, my code did't getting into shouldOverrideUrlLoading() method – Vamsi Smart May 26 '15 at 18:17
  • I recommend that you add `@Override` annotations to your `WebViewClient` methods, to ensure their signatures are correct. Also, bear in mind that `shouldOverrideUrlLoading()` is pretty much only used for simple links and server redirects. Other things, like forms or JavaScript-based redirects, will not trigger `shouldOverrideUrlLoading()`. – CommonsWare May 26 '15 at 18:19
  • @CommonsWare thank you so much, it works fine for links with .pdf extension after adding '@Override' annotations, but still it did't working on http://goir.ap.gov.in, i think they are not links but they works on crome browser. – Vamsi Smart May 26 '15 at 18:49

0 Answers0