I'm going to try responding with a new answer since my original answer was downgraded to a comment. I'm not sure why but I do have a solutions now for API 10 and under as well as API 11 and over. I'll post code to clarify.
For API 11 and over simply override ShouldInterceptRequest in your webview client,
For API 10 and under use this http listener, http://elonen.iki.fi/code/nanohttpd/,
Putting the two approaches together, the relevant pieces of code for me were:
final String assetPrefix = "file:///android_asset/";
final String httpPrefix = "http://localhost:8001";
// for me this part is in onCreateView after the webview settings bit...
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
webView.setWebViewClient(new HoneyCombWebViewClient());
else
webView.setWebViewClient(new MyWebViewClient());
webViewLoadUrl();
}
private void webViewLoadUrl() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
curURL = assetPrefix + curFileName;
webView.loadUrl(curURL);
}
else
{
curURL = httpPrefix + '/' + curFileName;
String str = assetFileToString(curFileName);
webView.loadDataWithBaseURL(httpPrefix, str, "text/html", "UTF-8", null);
}
}
// then for the WebViewClient
private class HoneyCombWebViewClient extends MyWebViewClient {
public WebResourceResponse shouldInterceptRequest(WebView view, String url)
{
// remove the asset prefix from the url
String fileName = url.substring(assetPrefix.length() - 1);
// ExpansionFiles is the pointer to your ZipResourceFile
InputStream inputStream = ExpansionFiles.getWebResource(fileName);
if (url.endsWith("jpg") || url.endsWith("png"))
return new WebResourceResponse("image/*", "base64", inputStream);
return super.shouldInterceptRequest(view, url);
}
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
else if (url.startsWith(httpPrefix)) {
curURL = url;
String str = assetFileToString(url.substring(httpPrefix.length() + 1));
webView.loadDataWithBaseURL(httpPrefix, str, "text/html", "UTF-8", null);
return true;
}
else if (url.startsWith(assetPrefix)){
curURL = url;
view.loadUrl(url);
return true;
}
// else.....
}
}
Finally, to use NanoHTTPD, find the methods serve and have it return the input stream to your file from the expansion file:
public Response serve( String uri, String method, Properties header, Properties parms, Properties files )
{
InputStream data = ExpansionFiles.getWebResource(uri);
String mimeType;
if (uri.endsWith("jpg") || uri.endsWith("png"))
mimeType = "base64";
else if (uri.endsWith("css"))
mimeType = "text/css";
else if (uri.endsWith("js"))
mimeType = "text/javascript";
else
mimeType = "text/html";
return new Response( HTTP_OK, mimeType, data );
}
Put in a call to the constructor somewhere at your toplevel:
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
// this for loading images from expansion file under webview in API 10
try
{
webServer = new NanoHTTPD(8001, new File("."));
}
catch( IOException ioe )
{
System.err.println( "Couldn't start server:\n" + ioe );
System.exit( -1 );
}
}
Oh, and you'll need to remove the main method from NanoHTTPD
I hope you find this helpful. It was a long and painful process for me....