@Android display /res/viewable in WebView

前端 未结 7 1451
耶瑟儿~
耶瑟儿~ 2020-11-29 05:51

I am throwing HTML to a webview to render. In the HTML I need to load an image that I have in /res/drawable.

I have /res/drawable/my_image.png

7条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 06:30

    I have the exact same problem, but neither placing the image file in res/drawable (which I first had to create, I hust had drawable-hdpi, -ldpi and -mdpi already existing) or using the file:///asset/ URL worked for me.

    Here is what I do:

    Activity:

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            mWebView = (WebView) findViewById(R.id.webview);
            mWebView.getSettings().setJavaScriptEnabled(true);
            String summary = "
    "; mWebView.loadData(summary, "text/html", "utf-8");

    Then in res/drawable I placed the banner300.jpg - but it does not show up. Any ideas?

提交回复
热议问题