how to load a picture from my resource in webview?
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
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, 2022Comments
-
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>
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");
-
wocmultimedia almost 13 yearsOk! It works I can scroll it but I cannot zoom into, what I can do
-
wocmultimedia almost 13 yearssorry 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 almost 13 yearsDoes the image be bigger then the screen size? cause this image is 480x320 pixel
-
wocmultimedia almost 13 yearsok I edited the code and get error MyWebViewClient cannot be resolved to a type
-
wocmultimedia almost 13 yearsok 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 almost 13 yearsOf 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 almost 13 yearsno, 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 almost 13 yearsno metter, there are many ways for doing it. just make a new question. Hope somebody or either me doing it.
-
user370305 almost 13 yearshhh.. Its not my answer. Friend..
-
-
wocmultimedia almost 13 yearsI tried it and get this error "Cannot make a static reference to the non-static method getResources() from the type Context"
-
wocmultimedia almost 13 yearsI 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 almost 13 yearsnothing at all, always the same error. I tried different combination but nothing
-
wocmultimedia almost 13 yearsyes, i put the image in assets and get the error is cannot find such directory or file /android_assets/map.png
-
wocmultimedia almost 13 yearsnothing always same error, do I have to add something in the manifest?
-
asenovm almost 13 yearsno, 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 almost 13 yearsI am using the emulator and I would like to insert it from different drawable folders to fit all densities
-
asenovm almost 13 yearsIsn'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 almost 13 yearsthanx 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 almost 13 yearsno 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 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 almost 13 yearsok, 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 almost 13 yearsVery sorry but the browser made a big mess
-
user370305 almost 13 yearsmake a new question in SO for use file from application's internal package/storage.
-
wocmultimedia almost 13 yearsHi...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 almost 13 yearswhich line myWebView.setWebViewClient(new WebViewClient());?
-
hgoebl about 8 yearsWho is upvoting this? It's just wrong and won't work.