因爲我的原始答案被降級爲評論,所以我會嘗試用新的答案進行回覆。我不知道爲什麼,但我現在有針對API 10和API 11以及以下的解決方案。我會張貼代碼澄清。
對於API 11及以上只需在您的WebView客戶端覆蓋ShouldInterceptRequest,
對於API 10,並在使用此http監聽器,http://elonen.iki.fi/code/nanohttpd/,
把兩種方法一起,代碼的相關作品對我來說分別爲:
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.....
}
}
最後,使用NanoHTTPD,找到方法服務,並將它輸入流從擴展文件返回給您的文件:
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);
}
投入到構造函數的調用你的頂層地方:
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);
}
}
哦,你需要從NanoHTTPD
去除的主要方法,我希望對您有所幫助。這對我來說是一個漫長而痛苦的過程......
你有沒有解決過這個問題?我有一個API 11和更大的解決方案,請參閱http://stackoverflow.com/questions/13073594/how-to-load-image-from-expansion-file-into-webview我使用webview.loadUrl但我已驗證它與webview.loadDataWithBaseURL的工作方式相同仍然需要針對API 10的解決方案,儘管 – 2012-10-30 16:36:22
無法解決此問題。我目前將obb解壓縮到SD卡並使用那裏的圖像。 – Javed 2012-11-28 14:50:33