Android add image to webview from a drawable

前端 未结 4 1225
梦毁少年i
梦毁少年i 2020-12-01 06:29

I\'d like something:

s=\"




\";

How can I do this?

相关标签:
4条回答
  • 2020-12-01 07:09

    After some experimenting I found that the following HTML worked on Android 2.3.1 (but not on older versions):

    <img src="file:///android_res/drawable/example.png"/>
    

    The really cool part is that the image file was actually in the directory drawable-hdpi but the resource manager provides the image in the standard directory drawable.

    0 讨论(0)
  • 2020-12-01 07:11

    You can only do such a thing if our image is inside your /assets folder. Also, you must load your html with a baseUrl that's inside your assets folder.

    You can use WebView.loadUrl() or WebView.loadDataWithBaseURL():

    webView.loadUrl("file:///android_asset/file.html");

    or

    webView.loadDataWithBaseURL("file:///android_asset/", "<img src='file.jpg' />", "text/html", "utf-8", null);
    

    (file.jpg should be inside your assets folder)

    0 讨论(0)
  • 2020-12-01 07:16

    Set Image Width 100%.

    wv.loadDataWithBaseURL("file:///android_res/drawable/", "<img src='"+ url + "' style='width:100%' />", "text/html", "utf-8", null); 
        wv.getSettings().setBuiltInZoomControls(true);
        wv.getSettings().setDisplayZoomControls(false);
    
    0 讨论(0)
  • 2020-12-01 07:19

    Try this:

     web_object.loadDataWithBaseURL("file:///android_res/drawable/", "<img src='test.jpg' />", "text/html", "utf-8", null);
    

    no need to make seprate html file

    0 讨论(0)
提交回复
热议问题