加载图像url到webview



我试图从http://loremflickr.com/加载图像到webview,但它似乎不能在android 4.3上工作。有人能向我解释为什么以及如何解决这个问题吗?在Android 4.4和Android 5中可以正常工作,但在Android 4.3中不行

下面是我的代码:

@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!

你错在哪里?

不要使用

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

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

 @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);
    }
}
详情

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

你可以试试:

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

按此加载图像,它肯定会起作用

相关内容

  • 没有找到相关文章

最新更新