Android Webview css无法显示背景网址图片

时间:2015-07-31 06:20:56

标签: android html css webview

Android拦截请求基于http的本地存储css .. webview获取css文件。但无法获得css 背景:网址(' image.jpg')。我存储本地资产文件夹css和jpg相同的位置,但不能得到...

以下编码:

 private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSupportActionBar().hide();
    setContentView(R.layout.activity_main);

    mWebView = (WebView) findViewById(R.id.webView);
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public WebResourceResponse shouldInterceptRequest(final WebView view, String url) {
            if (url.contains("tamil.css")) {
                return getCssWebResourceResponseFromAsset();
            } else {
                return super.shouldInterceptRequest(view, url);
            }
        }

        private WebResourceResponse getCssWebResourceResponseFromAsset() {
            try {
                return getUtf8EncodedCssWebResourceResponse(getAssets().open("tam.css"));
            } catch (IOException e) {
                return null;
            }
        }

        private WebResourceResponse getUtf8EncodedCssWebResourceResponse(InputStream data) {
            return new WebResourceResponse("text/css", "UTF-8", data);
        }
    });

    mWebView.loadUrl("http://www.example.com/Mobile/mob/aa.html");
}

css文件:

body{background:url('tamil.jpg');}
h1{color:#cfc;}

0 个答案:

没有答案