0

I have an android application that is just a wrapper around my mobile website. I have one webview that simply points to the login URL of my site. When I access my mobile site on a regular browser I can download the PDFs when I click on a button (via href="url that returns pdf"). On my wrapper however, I cannot download it. I assume this has something to do with my web settings or some method that I need to override. How can I get my wrapper to download a file like a regular browser?

code:

public class MainActivity extends Activity {



private static final String TAG = "TAG";
private WebView myWebViewGlobal=null;

@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    //Remove title bar as we already have it in the web app

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    //Point to the content view defined in XML

    setContentView(R.layout.main);

    //Configure the webview setup in the xml layout
    WebView myWebView=(WebView) findViewById(R.id.webview);
    myWebViewGlobal=myWebView;
    WebSettings webSettings = myWebView.getSettings();

    //Yes, we want javascript, pls.

    webSettings.setJavaScriptEnabled(true);
    webSettings.setAllowFileAccess(true);
    myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

    myWebView.setDownloadListener(new DownloadListener() 
    {
        public void onDownloadStart(String url, String userAgent,
                        String contentDisposition, String mimetype,
                        long contentLength) {



                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent); 
    }
    });

    if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.ECLAIR) {
        try {
            Log.d(TAG, "Enabling HTML5-Features");
            Method m1 = WebSettings.class.getMethod("setDomStorageEnabled", new Class[]{Boolean.TYPE});
            m1.invoke(webSettings, Boolean.TRUE);

            Method m2 = WebSettings.class.getMethod("setDatabaseEnabled", new Class[]{Boolean.TYPE});
            m2.invoke(webSettings, Boolean.TRUE);

            Method m3 = WebSettings.class.getMethod("setDatabasePath", new Class[]{String.class});
            m3.invoke(webSettings, "/data/data/" + getPackageName() + "/databases/");

            Method m4 = WebSettings.class.getMethod("setAppCacheMaxSize", new Class[]{Long.TYPE});
            m4.invoke(webSettings, 1024*1024*8);

            Method m5 = WebSettings.class.getMethod("setAppCachePath", new Class[]{String.class});
            m5.invoke(webSettings, "/data/data/" + getPackageName() + "/cache/");

            Method m6 = WebSettings.class.getMethod("setAppCacheEnabled", new Class[]{Boolean.TYPE});
            m6.invoke(webSettings, Boolean.TRUE);

            Method m7 = WebSettings.class.getMethod("setAllowContentAccess", new Class[]{Boolean.TYPE});
            m7.invoke(webSettings, Boolean.TRUE);

            Method m8 = WebSettings.class.getMethod("setAllowFileAccessFromFileURLs", new Class[]{Boolean.TYPE});
            m8.invoke(webSettings, Boolean.TRUE);

            Method m9 = WebSettings.class.getMethod("setAllowUniversalAccessFromFileURLs", new Class[]{Boolean.TYPE});
            m9.invoke(webSettings, Boolean.TRUE);

            Method m10 = WebSettings.class.getMethod("setLoadsImagesAutomatically", new Class[]{Boolean.TYPE});
            m10.invoke(webSettings, Boolean.TRUE);

            Method m11 = WebSettings.class.getMethod("setLoadsImagesAutomatically", new Class[]{Boolean.TYPE});
            m11.invoke(webSettings, Boolean.TRUE);

            Log.d(TAG, "Enabled HTML5-Features");
        }
        catch (NoSuchMethodException e) {
            Log.e(TAG, "Reflection fail", e);
        }
        catch (InvocationTargetException e) {
            Log.e(TAG, "Reflection fail", e);
        }
        catch (IllegalAccessException e) {
            Log.e(TAG, "Reflection fail", e);
        }
    }

    //Make sure links in the webview is handled by the webview and not sent to a full browser

    myWebView.setWebViewClient(new WebViewClient());

    //And let the fun begin

    myWebView.loadUrl("http://mywebsite.com/login.php");

}

@Override  
public void onBackPressed()   
{  
    if(myWebViewGlobal.canGoBack())
    {
        myWebViewGlobal.goBack();
    }

    return;  
}  

}

user1467225
  • 119
  • 1
  • 1
  • 10
  • What do you mean by cannot download?? Are you getting any exceptions?? Specifically NetworkOnMainThread?? – Robin Chander Dec 03 '12 at 21:45
  • @Robin Chander No, its just that when I access the same page in a regular browser I get the "starting download" toast and it downloads the file. When I click the same button in my wrapper, it does nothing – user1467225 Dec 03 '12 at 21:54

0 Answers0