2012-08-16 60 views
2

我有一個顯示地圖的android應用程序。這些地圖是高分辨率圖像,爲了保持分辨率,這些圖像被分割成每個地圖100個圖塊我目前每個地圖都位於資產文件夾的單獨文件夾中。由於這使得我的apk很大,我想將這些圖像移動到我的主apk擴展文件中。 目前我使用下面的代碼縫瓷磚在網頁視圖顯示在網頁視圖圖像一起:使用android webview中的apk擴展文件中的圖像

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擴展文件?是否有可能使用圖像,而沒有解壓縮到SD卡的obb?

+0

你有沒有解決過這個問題?我有一個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

+0

無法解決此問題。我目前將obb解壓縮到SD卡並使用那裏的圖像。 – Javed 2012-11-28 14:50:33

回答

3

因爲我的原始答案被降級爲評論,所以我會嘗試用新的答案進行回覆。我不知道爲什麼,但我現在有針對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

去除的主要方法,我希望對您有所幫助。這對我來說是一個漫長而痛苦的過程......

+1

NanoHTTPD已棄用服務(字符串,字符串,屬性...)有利於服務(IHTTPSession),但您可以獲得所有屬性相同 - IHTTPSession.getUri()等。 – AndyC 2013-09-23 18:41:41

1

這是使用JOBB工具打包擴展文件並使用StorageManager來掛載它的好替代方案。之後,擴展文件中的文件應該可以作爲文件系統中的常規文件訪問。在這種情況下不需要解壓擴展文件。

+0

謝謝!我認爲這是一個很好的方法。我寫了一篇博客文章,描述了我是如何實現它的:http://saveme-dot-txt.blogspot.com/2016/06/setting-up-google-play-expansion-file.html – 2016-06-14 14:08:24