Are there any examples on how to use WebP for Android? Im trying to get a list of webp images and show them in a listview with an imageview.
I know theres a libwebp api and I have integrated it into my Android project using the NDK, but how do I excatly use the api to take my binary webp and show it in an imageview?
Any help would be amazing!
Use libwebp with NDK. libwebp-0.1.3 already comes with Android.mk file (outdated and with syntax errors, but still). It also got generated JNI bindings in
/swig/
directory.Here's how I got it working:
PATH
.your_project_dir/jni
Android.mk
with the one below.jni/src/libwebp_java_wrap.c
with content from below.jni/Application.mk
, with content from below.ndk-build
from project directory. This generates.so
files in/libs/
. You can inspect them withnm -D libs/armeabi/libwebp.so
. In the list you'll see both the native library functions (likeWebPDecodeRGB
) and their JNI counterparts (likeJava_com_google_webp_libwebpJNI_WebPDecodeRGB
)/jni/swig/libwebp.jar
to build path of your Android projectHere's content for Android.mk. Changed from original: removed encoder bits as I don't need these, added
libwebp_java_wrap.c
, changedinclude $(BUILD_STATIC_LIBRARY)
toinclude $(BUILD_SHARED_LIBRARY)
.Content for libwebp_java_wrap.c is here, it's basically the same as bundled in libwebp tarball, except encoder bits removed.
Content for Application.mk:
Here's how to use in Java code. Notice how it converts byte[] array to int[] color array--this will break if endianness changes, right? Also notice how it uses arrays instead of single integers for width and height so they are passed by reference.
There is currently no way to display a webp image on any native app on an Android device, including the web browser. You will have to look into 3rd party apps to display these images.
According to the WebP mailing list, they are working on incorporating WebP support into the Android SDK. They did not say when exactly they plan to release this but when they do you should be able to save bitmaps as WebP format as well as JPEG and PNG.
EDIT: Android 4.0 aka Ice Cream Sandwich now comes with native support for the WebP format. You can see supported file types at the Android developer site.
WebP is supported for Android 4.0+, a.k.a. API level 14. You can check using
android.os.Build.VERSION >= 14
.We've written an Android library just for that.
https://github.com/EverythingMe/webp-android
webp-android is a library we use at EverythingMe since we love webp. We use it to save bandwidth as well as shrinking our APK sizes.
webp-android is an adaptation of chromium's webp decoder, and an addition of a JNI wrapper to easily use it it in your java code. It's also easy to load webp images from xml to an ImageView (with the included
WebpImageView
) like so:Google claims that WebP is supported starting from Android 4.0+ ( http://developer.android.com/guide/appendix/media-formats.html ), however in our own tests webp images show as blue questionmarks both in the standard browser and Chrome on Android 4.0 and 4.1. On Android 4.2 WebP images seem to be rendered ok in a webview and in google chrome.