Reputation: 269
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");
Upvotes: 1
Views: 11735
Reputation: 2048
You can access like this:-
file:///android_res/drawable/YOUR_FILE_NAME
Upvotes: 13
Reputation: 109237
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
Upvotes: 6