how to load a picture from my resource in webview?

11,185

Solution 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

Solution 2

You can access like this:-

file:///android_res/drawable/YOUR_FILE_NAME

Share:
11,185
wocmultimedia
Author by

wocmultimedia

Founder and Owner of World On Communications, specialized in multimedia productions, video editing teacher with Avid software, just starting to learn developing in Android to port my products on this system like simple app to increase tourist and cultural travels.

Updated on June 04, 2022

Comments

  • wocmultimedia
    wocmultimedia about 2 years

    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
      wocmultimedia almost 13 years
      Ok! It works I can scroll it but I cannot zoom into, what I can do
    • wocmultimedia
      wocmultimedia almost 13 years
      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
      wocmultimedia almost 13 years
      Does the image be bigger then the screen size? cause this image is 480x320 pixel
    • wocmultimedia
      wocmultimedia almost 13 years
      ok I edited the code and get error MyWebViewClient cannot be resolved to a type
    • wocmultimedia
      wocmultimedia almost 13 years
      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
      wocmultimedia almost 13 years
      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?
    • user370305
      user370305 almost 13 years
      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
      user370305 almost 13 years
      no metter, there are many ways for doing it. just make a new question. Hope somebody or either me doing it.
    • user370305
      user370305 almost 13 years
      hhh.. Its not my answer. Friend..
  • wocmultimedia
    wocmultimedia almost 13 years
    I tried it and get this error "Cannot make a static reference to the non-static method getResources() from the type Context"
  • wocmultimedia
    wocmultimedia almost 13 years
    I tried but I get another error the requested filewas not found. No content Provider: content://44f3f600map can you write the complete line code for my file called map.png in drawable resource?
  • wocmultimedia
    wocmultimedia almost 13 years
    nothing at all, always the same error. I tried different combination but nothing
  • wocmultimedia
    wocmultimedia almost 13 years
    yes, i put the image in assets and get the error is cannot find such directory or file /android_assets/map.png
  • wocmultimedia
    wocmultimedia almost 13 years
    nothing always same error, do I have to add something in the manifest?
  • asenovm
    asenovm almost 13 years
    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")
  • wocmultimedia
    wocmultimedia almost 13 years
    I am using the emulator and I would like to insert it from different drawable folders to fit all densities
  • asenovm
    asenovm almost 13 years
    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
  • wocmultimedia
    wocmultimedia almost 13 years
    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
  • user370305
    user370305 almost 13 years
    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:-)
  • wocmultimedia
    wocmultimedia almost 13 years
    :-) 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
  • user370305
    user370305 almost 13 years
    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")
  • wocmultimedia
    wocmultimedia almost 13 years
    Very sorry but the browser made a big mess
  • user370305
    user370305 almost 13 years
    make a new question in SO for use file from application's internal package/storage.
  • wocmultimedia
    wocmultimedia almost 13 years
    Hi...I found the solution and I share it with you this is the code just a line was missing myWebView.setWebViewClient(new WebViewClient()); myWebView.loadUrl("file:///android_asset/map.png"); setContentView(myWebView);
  • user370305
    user370305 almost 13 years
    which line myWebView.setWebViewClient(new WebViewClient());?
  • hgoebl
    hgoebl about 8 years
    Who is upvoting this? It's just wrong and won't work.