how to load a picture from my resource in webview?

筅森魡賤 提交于 2019-12-11 10:56:57

问题


I want to load a picture called map.png set in my drawable resource in the WebView.

I found in another answer this suggestion webview.loadUrl("file://...") but I don't understand how to set it properly. I always get error that the requested file was not found. This is what I wrote

`public class Map extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
  WebView mWebView =(WebView)findViewById(R.id.webMap);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("file://res/drawable/map");`

This is the XML

    <WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webMap"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:orientation="vertical">

</WebView>

     setContentView(R.layout.map);
  WebView mWebView =(WebView)findViewById(R.id.webMap);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("file:///android_assets/map)");

This is a complete screen shot

This is the new code but I get an error

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        WebView myWebView = (WebView) findViewById(R.id.webMap);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.getSettings().setBuiltInZoomControls(true);
        myWebView.setWebViewClient(new MyWebViewClient());
        myWebView.loadUrl("file://mnt/sdcard/map.png");

回答1:


you can not access Drawable from application's drawable directory like

   mWebView.loadUrl("file://res/drawable/map");

for file from your asset directory of your app package you should use

   mWebView.loadUrl("content://"+Context.getResources().getAsset()+"filename");

EDIT: ok put your image in Applications asset directory, then for example write line,

  myWebView.loadUrl("file:///android_asset/testimage.jpg"); 

or use

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.loadUrl("file://mnt/sdcard/map.png");

try it




回答2:


You can access like this:-

file:///android_res/drawable/YOUR_FILE_NAME




回答3:


Try with

mWebView.loadUrl("file://res/drawable/map.png")


来源:https://stackoverflow.com/questions/7364197/how-to-load-a-picture-from-my-resource-in-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!