Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading an Android Resource into a WebView

I have an Android app that displays a comic book. To make use of the built-in zoom controls, I am loading the pictures in a WebView like so:

webView.loadUrl("file:///android_asset/page1.jpg"); 

This is working just fine, however, since the images are in the assets folder, they are not being compressed which makes my .apk enormous. I was wondering how to reference resource files (from the res/drawable folder) with a file path like I did above with the assets. Does anyone know what that path would look like? I've tried things like "file:///res/drawable/pagetitle.jpg" with no success. Thanks for the help.


Update:

I found that "file:///android_res/drawable/page1.jpg" was the path that I was looking for.

like image 894
Amplify91 Avatar asked Jan 31 '11 19:01

Amplify91


People also ask

How do I load WebView?

Modify src/MainActivity. java file to add WebView code. Run the application and choose a running android device and install the application on it and verify the results. Following is the content of the modified main activity file src/MainActivity.

Is it possible to get data from HTML forms into Android while WebView?

Yes you can, you can use javascript to get webpage content. Then use the webview jsInterface to return the content to you java code.

How do you communicate between WebView and native Android?

2.1 To receive data from webview ,we can create an interface, which will enable webview to connect the native layer and pass data. From native layer, create a class and replicate the following. While configuring web view, we need to set JavaScript interface as above JSBridge class.

Does local storage work in WebView?

html file informs me that local storage is'nt supported by my browser (ie. the webview ). If you have any suggestion.. @Maetschl: if you want to change the code block from intentations to backticks, then at least remove the leading spaces.


2 Answers

from this site

Using the resource id, the format is:

"android.resource://[package]/[res id]"  Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/" + R.raw.myvideo); 

or, using the resource subdirectory (type) and resource name (filename without extension), the format is:

"android.resource://[package]/[res type]/[res name]"  Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/raw/myvideo"); 
like image 83
runor49 Avatar answered Sep 19 '22 21:09

runor49


I also had to use loadDataWithBaseURL instead of just plain loadData to get the file:///android_res/drawable/page1.jpg to work.

like image 29
Sean Moore Avatar answered Sep 20 '22 21:09

Sean Moore