简体   繁体   中英

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.

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");

我还必须使用loadDataWithBaseURL而不是简单的loadData来使file:///android_res/drawable/page1.jpg正常工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM