WebView with zoom controls, image from SDCARD


SDK Version: 
M3

The Problem:
When I'm trying to load an image (tried both gif and png) from my assets directory, into a webview.
In my assets folder, I have an image called myImage.gif
Here's my wrong code:

  1. WebView data = (WebView) findViewById(R.id.data)
  2. data.loadData("<IMG HEIGHT=\"42px\" WIDTH=\"42px\" SRC=\"file:///android_assets/myImage.gif\" />", "text/html",  "UTF-8");

All that happens when I try it, is that I get a blank screen with a
empty 42px/42px box.
In logcat, I see the message:
03-30 00:21:14.398: DEBUG/WebCore(214): Console: Not allowed to load
local resource: file:///android_assets/myImage.gif line: 0 source:

The Solution:
Even though, I'm going to load the image file from the filesystem (SDCARD or Phone Storage), instead of Assets folder, I've found a solution for this, and you don't even have to care about ContentProviders... (Which is also a good solution, but this is much easier & quicker in this case.)
The key is WebView's loadDataWithBaseURL method, but let's see an example:
  1. /* Using WebView to display the full-screen image */
  2. WebView full = (WebView)findViewById(R.id.webview);
  3.  
  4. /* Set up the Zoom controls */
  5. FrameLayout mContentView = (FrameLayout) getWindow().
  6. getDecorView().findViewById(android.R.id.content);
  7. final View zoom = this.full.getZoomControls();
  8. mContentView.addView(zoom, ZOOM_PARAMS);
  9. zoom.setVisibility(View.VISIBLE);
  10.  
  11. /* Create a new Html that contains the full-screen image */
  12. String html = new String();
  13. html = ("<html><center&gt;<img src=\""+fileName+"\"></html>" );
  14.        
  15. /* Finally, display the content using WebView */
  16. full.loadDataWithBaseURL("file:///sdcard/data/data/com.youproject.example/",
  17.                                                         html,
  18.                                                         "text/html",
  19.                                                         "utf-8",
  20.                                                         "");

Hope you found it useful, let me know how it works for you!