48

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.

Community
  • 1
  • 1
Amplify91
  • 2,690
  • 5
  • 24
  • 32
  • 7
    I just don't get your idea of further compressing jpg images. They're already compressed! Also, images in res folder are also not being compressed by Android build tools, because the tools are clever and avoid compressing jpg/png images twice, which makes no sense. – Pointer Null Jun 04 '12 at 10:26
  • I dont think that there is any other direct way. But u can try using [loadData](http://developer.android.com/reference/android/webkit/WebView.html#loadData%28java.lang.String,%20java.lang.String,%20java.lang.String%29) method. – the100rabh Jan 31 '11 at 19:50
  • @PointerNull PNG recompression can make a lot of sense, see http://addyosmani.com/blog/image-optimization-tools/ - recompressing lossy JPG again is to be avoided under all circumstances unless one considers the artefacts as modern art... – Philzen Apr 22 '14 at 15:33
  • 2
    @Philzen: we're talking about ZIP compression of JPG or PNG, which is useless because zipped JPG or PNG is of same size (+-1%) as original. Your article discusses something else. – Pointer Null Apr 22 '14 at 15:37

2 Answers2

22

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");
Sandip Armal Patil
  • 6,241
  • 21
  • 93
  • 160
runor49
  • 3,870
  • 1
  • 21
  • 18
  • 1
    I found a different solution (see the update) and I haven't tried this, but thank you for the response. – Amplify91 Feb 02 '11 at 22:02
  • 2
    Using `android.resource://com.androidbook.samplevideo/" + R.raw.myvideo` with `webView.loadUrl` doesn't seem to be working. – Indrek Kõue Jun 27 '12 at 14:02
  • 16
    use `file:///android_res/raw/myvideo.mp4` for webview. – Jeffrey Blattman Nov 28 '12 at 01:35
  • 2
    This is the accepted answer, yet I'm unable to see how it answers the question. The question asked "how to reference resource files ... with a file path", but this answer gives a non-`file` `Uri`. The question asks about loading a resource, and shows the use of `webView.loadUrl(String)`, but this answer doesn't tell how to load a resource, and a `Uri` can't be passed directly to `loadUrl()`. – LarsH Jul 29 '18 at 00:59
6

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

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Sean Moore
  • 181
  • 2
  • 4