简体   繁体   中英

Loading image url into webview

I'm trying to load image into a webview from http://loremflickr.com/ , but it doesn't seem to work on android 4.3 . Can anyone explain to me why and how can i fix this ? it work fine for Android 4.4 and Android 5 but not 4.3

Here's my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    loadUrlInWebView("http://loremflickr.com/320/240/dog");
}
private void loadUrlInWebView(String _URL) {
    WebView mWebView = (WebView) findViewById(R.id.webviewer);
    mWebView.setBackgroundColor(Color.TRANSPARENT);
    mWebView.setVerticalScrollBarEnabled(false);
    mWebView.setHorizontalScrollBarEnabled(false);
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mWebView.setWebChromeClient(new WebChromeClient());
    mWebView.setWebViewClient(new MyWebViewClient());
    mWebView.loadUrl(_URL);
}

private class MyWebViewClient extends WebViewClient {

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        Log.e("TAG", "error code " + errorCode);
    }
}

Logcat

///logcat 
09-30 14:32:33.387    4309-4309/com.myapplication D/dalvikvm﹕ Late-enabling CheckJNI
09-30 14:32:35.395    4309-4309/com.myapplication D/libEGL﹕ loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
09-30 14:32:35.582    4309-4309/com.myapplication D/libEGL﹕ loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
09-30 14:32:35.621    4309-4309/com.myapplication D/libEGL﹕ loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
09-30 14:32:36.035    4309-4309/com.myapplication D/OpenGLRenderer﹕ Enabling debug mode 0
09-30 14:32:36.199    4309-4309/com.myapplication D/TAG﹕ onLoadResource http://loremflickr.com/320/240/dog
09-30 14:32:37.246    4309-4309/com.myapplication D/TilesManager﹕ Starting TG #0, 0x59fe9ae8
09-30 14:32:37.246    4309-4309/com.myapplication D/TilesManager﹕ new EGLContext from framework: 59ebc0c0
09-30 14:32:37.246    4309-4309/com.myapplication D/GLWebViewState﹕ Reinit shader
09-30 14:32:37.262    4309-4309/com.myapplication D/GLWebViewState﹕ Reinit transferQueue
09-30 14:32:37.317    4309-4398/com.myapplication W/PicturePileLayerContent﹕ Warning: painting PicturePile without content!
09-30 14:32:37.324    4309-4398/com.myapplication W/PicturePileLayerContent﹕ Warning: painting PicturePile without content!
09-30 14:32:37.324    4309-4398/com.myapplication W/PicturePileLayerContent﹕ Warning: painting PicturePile without content!

Whats your mistake:

Don't use

loadUrlInWebView("http://loremflickr.com/320/240/dog");

Use

loadUrl("http://loremflickr.com/320/240/dog");

Finally

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    loadUrlInWebView();
}
private void loadUrlInWebView() {
    WebView mWebView = (WebView) findViewById(R.id.webviewer);
    mWebView.loadUrl("http://loremflickr.com/320/240/dog");
    mWebView.setBackgroundColor(Color.TRANSPARENT);
    mWebView.setVerticalScrollBarEnabled(false);
    mWebView.setHorizontalScrollBarEnabled(false);
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mWebView.setWebChromeClient(new WebChromeClient());
    mWebView.setWebViewClient(new MyWebViewClient());
    mWebView.getSettings().setUseWideViewPort (true);

}

private class MyWebViewClient extends WebViewClient {

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        Log.e("TAG", "error code " + errorCode);
    }
}

For details:
https://developer.android.com/reference/android/webkit/WebView.html

You can try this:

WebView mWebView = (WebView) findViewById(R.id.webviewer);
mWebView.loadUrl("http://loremflickr.com/320/240/dog");

Just load image by this, it will definitely work.

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