简体   繁体   中英

how to load a picture from my resource in webview?

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");

You can access like this:-

file:///android_res/drawable/YOUR_FILE_NAME

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

试试看

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM