@Android display /res/viewable in WebView

前端 未结 7 1445
耶瑟儿~
耶瑟儿~ 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:40

    I admit I don't know much about WebViews / HTML, but it seems like you're taking the more complicated route for this. There's an easy way to load the HTML file; put it in your assets folder, and then it can be referred to as follows.

    WebView webView = new WebView(this);
    webView.loadUrl("file:///android_asset/index.html");
    setContentView(webView);
    

    Obviously your layout is more complex as you have more than just the WebView so you can't use setContentView() directly, but that's the basic idea. Then to reference an image in that HTML file, I used a tag like so:

    
    

    Does that work for you?

提交回复
热议问题