1

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>

enter image description here

     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

enter image description here

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");
wocmultimedia
  • 259
  • 1
  • 6
  • 18
  • Ok! It works I can scroll it but I cannot zoom into, what I can do – wocmultimedia Sep 09 '11 at 18:57
  • sorry how? I double click inside the picture but nothing happen and besides a stupid question even if will work how it would be fit in the apk file? – wocmultimedia Sep 09 '11 at 19:03
  • Does the image be bigger then the screen size? cause this image is 480x320 pixel – wocmultimedia Sep 09 '11 at 19:09
  • ok I edited the code and get error MyWebViewClient cannot be resolved to a type – wocmultimedia Sep 09 '11 at 19:18
  • ok I found the error was MyWebViewClient I changed in WebViewClient and imported the the widget. Please, answer my last question to package it in apk with this formula it's enough I export as usual or I have to do something more? – wocmultimedia Sep 09 '11 at 19:22
  • Of course, it's the minimum I can do. Please, tell me it means that if I need to set another picture in another webview I can push in the same way in the SDcard via DDMS and I will found both in my final apk package? – wocmultimedia Sep 09 '11 at 19:29
  • no, its not contain in your apk package. Its only for your current emulator. If you want to put the file in internal package then you want to use of either apps Asset directory or internal storage file. – user370305 Sep 09 '11 at 19:32
  • no metter, there are many ways for doing it. just make a new question. Hope somebody or either me doing it. – user370305 Sep 09 '11 at 19:41
  • hhh.. Its not my answer. Friend.. – user370305 Sep 09 '11 at 19:50

3 Answers3

13

You can access like this:-

file:///android_res/drawable/YOUR_FILE_NAME

vntstudy
  • 2,038
  • 20
  • 24
6

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

user370305
  • 108,599
  • 23
  • 164
  • 151
0

Try with

mWebView.loadUrl("file://res/drawable/map.png")
asenovm
  • 6,397
  • 2
  • 41
  • 52
  • nothing always same error, do I have to add something in the manifest? – wocmultimedia Sep 09 '11 at 17:52
  • no, no need to do that. Try uploading the picture to the external storage of the device and load it from there - i.e `mWebView.loadUrl("file://mnt/sdcard/map.png")` – asenovm Sep 09 '11 at 18:03
  • I am using the emulator and I would like to insert it from different drawable folders to fit all densities – wocmultimedia Sep 09 '11 at 18:08
  • Isn't there a way to upload file to the emulator? I think there is? Try loading from the external storage - you can use one image and scale it accordingly from the code to fit all densities. I had to implement this loading of offline content a few months ago and this totally worked. But i was loading from the external storage - maybe there's some issue with loading from the drawables folder although there's no logical reason for it, I think :D – asenovm Sep 09 '11 at 18:13
  • thanx I don't know how to do. I think I can access Resources in someway but I'm novice so it's a bit harder – wocmultimedia Sep 09 '11 at 18:21
  • no its not harder, we suggest the way its working fine in our case. We can't figureout why its not working for you. Anyway we are trying to resolve it. Thanks:-) – user370305 Sep 09 '11 at 18:26
  • :-) your help is important to understand this process. In my mind I supposed it was easy to set a picture in a webview to zoom in out. It's easy if it is online because the Url is set directly but I cannot retrieve a picture from my project and set into the webview – wocmultimedia Sep 09 '11 at 18:30
  • ok, now try again, from eclipse IDE -> DDMS -> select EMULATOR -> from top left PUSH map.png file in sdcard -> OK after push it in sdcard now use mWebView.loadUrl("file://mnt/sdcard/map.png") – user370305 Sep 09 '11 at 18:46
  • Who is upvoting this? It's just wrong and won't work. – hgoebl Jun 08 '16 at 07:05