使用从APK扩展文件中的Android的WebView图片
我有一个Android应用程序,显示地图。地图是切成100瓦每地图,以保持分辨率高清晰度图像。我现在有在我的资产文件夹中的单独的文件夹每个地图。由于这使我的APK巨大的,我想将这些图片到我的主APK扩展文件。 目前我使用下面的code拼接瓷砖一起在web视图显示在一个web视图图像:
I have an android app that displays maps. The maps are hi-res images sliced to 100 tiles per map to maintain resolution. I currently have each map in a separate folder in my assets folder. As this makes my apk huge, I want to move these images to my main apk expansion file. Currently I am displaying the images in a webview using the following code to stitch the tiles together in the webview:
class ShowMapTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... maps) {
String html = "<html><table cellpadding=\"0\" border=\"0\" cellspacing=\"0.0\">";
for (int i = 0; i < 10; i++) {
html += "<tr>";
for (int j = 0; j < 10; j++)
html += "<td><img src=\"maps/" + maps[0] + "/slice_" + i
+ "_" + j + ".png\"></td>";
html += "</tr>";
}
return html + "</table></html>";
}
@Override
protected void onPostExecute(String result) {
WebView wv = (WebView) context.findViewById(R.id.webView);
wv.getSettings().setSupportZoom(true);
wv.getSettings().setBuiltInZoomControls(true);
wv.getSettings().setUseWideViewPort(true);
wv.getSettings().setLoadWithOverviewMode(true);
wv.loadDataWithBaseURL("file:///android_asset/", result,
"text/html", "utf-8", null);
super.onPostExecute(result);
}
}
我如何迁移到APK扩展文件?是否有可能使用的图像而无需解压OBB到SD卡?
How do I migrate to the apk expansion file? Is it possible to use the images without having the unzip the obb to the sdcard?
我会尝试用新的答案回答,因为我原来的答案被降级的注释。我不知道为什么,但我有一个解决方案,现在API 10下,以及API 11以上。我会后code澄清。
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.
有关API 11以上只是覆盖ShouldInterceptRequest在你的web视图的客户端,
For API 11 and over simply override ShouldInterceptRequest in your webview client,
有关API的10,并在使用此HTTP监听器, HTTP://elonen.iki。 FI / code / nanohttpd / ,
For API 10 and under use this http listener, http://elonen.iki.fi/code/nanohttpd/,
把两种方法一起,相关件code对我来说是:
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.....
}
}
最后,使用NanoHTTPD,找到方法服务,并将它从扩展文件返回输入流文件:
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 );
}
}
哦,你需要从NanoHTTPD去除的主要方法
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....