简体   繁体   中英

Android/WebView doesn't load javascript gallery images when navigated to

I have a website/web app built with jquery mobile that I am trying to package into a webview for android; I am able to load up the pages locally by putting the whole site into the assets folder and loading the url like so:

mWebView.loadUrl("file:///android_asset/www/btec/index.html");

One of the pages is a gallery/slideshow of images that flips through automatically using javascript. However, if I navigate to my gallery from index.html, the images dont load. If I load it directly (ie load it directly into the webview with:

mWebView.loadUrl("file:///android_asset/www/btec/gallery.html");

the images appear and scroll no problem! I'm not sure why this is happening. Is there a setting I need to enable/disable? I have the following settings enabled already:

mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.getSettings().setLoadsImagesAutomatically(true);
mWebView.getSettings().setPluginsEnabled(true);

This a trick that probably solves your problem.

1) Create a WebViewClient class, overriding shouldOverrideUrlLoading method:

private class MyClient extends WebViewClient {
    private WebView mwv;

    public MyClient(WebView v) {
        mwv = v;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(url.equals("file:///android_asset/www/btec/gallery.html")) {
            mwv.loadUrl(url);
            return true;
        }
        return false;
    }
}

2) Associate this client to your webview:

mWebView.setWebViewClient(new MyClient(mWebView));

Probably not the best way; but I think it works... ;)

EDIT: for other details, you can see this: http://developer.android.com/reference/android/webkit/WebViewClient.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM