2011-06-01 94 views
1

在我的代碼中,我有一個自定義的緩存目錄設置爲SD卡,我指向'WebView.getSettings()。setAppCachePath(「」)',但操作系統將緩存發送到默認緩存(/ data/data /com.your.package.appname/cache)。自定義目錄正確創建的位置應該是,但數據不會進入它。我使用自定義目錄的原因是緩存的大小和臨時性,所以我需要用戶能夠刪除特定的緩存。WebView緩存數據目錄?

我在這裏錯過了什麼嗎?任何幫助,代碼或建議將一如既往地受到讚賞。

謝謝你。

public class DocumentView extends Activity { 
/** 
* -- Called when the activity is first created. 
* ================================================ 
**/ 
@Override 
public void onCreate(final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

String l = getIntent().getStringExtra("LABEL"); 
    CreateCacheDirectory(); 

    /** 
    * -- Set up the WebView 
    * ======================================================== 
    **/ 

    final String url = getIntent().getStringExtra("url");// get url that 
                 // passed from 
                 // previous 
                 // Activity 
    mWebView = (WebView) findViewById(R.id.webView1); 

    mWebView.getSettings().setDomStorageEnabled(true); 

    // Set cache size to 8 mb by default. should be more than enough 
    mWebView.getSettings().setAppCacheMaxSize(1024 * 1024 * 8); 

    // The DEFAULT location for the cache 
    mWebView.getSettings().setAppCachePath(extStorageDirectory 
      + "/myCompany/myApp/cache/" + l); 
    mWebView.getSettings().setAllowFileAccess(true); 
    mWebView.getSettings().setAppCacheEnabled(true); 
    mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); 

    mWebView.loadUrl(url); 

    mWebView.setWebChromeClient(new WebChromeClient() { 
     public void onProgressChanged(WebView view, int progress) { 
      activity.setProgress(progress * 100); 
      pd.show(); 

      if (progress == 100) 
       pd.dismiss(); 
     } 

     @Override 
     public void onReachedMaxAppCacheSize(long spaceNeeded, 
       long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { 
      quotaUpdater.updateQuota(spaceNeeded * 2); 
     } 

    }); 

    mWebView.setWebViewClient(new WebViewClient() { 
     @Override 
     public void onReceivedError(WebView view, int errorCode, 
       String description, String failingUrl) { 
      // The redirect 
      if (url.equals(failingUrl)) { 
       // error.html is the place we are redirected to by the 
       // server if we are online 
       mWebView.loadUrl("http:www.aero-technologies.us/error.html"); 
       return; 
      } else if (url.equals(failingUrl)) { // The cache failed – We 
               // don't have an offline 
               // version to show 
       // This code removes the ugly android's "can't open page" 
       // and simply shows a dialog stating we have no network 
       view.loadData("", "text/html", "UTF-8"); 
       // TODO -->showDialog(DIALOG_NONETWORK); 
      } 
     } 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      view.loadUrl(url); 
      return true; 
     } 
    }); 

public void CreateCacheDirectory() { 
    if (android.os.Environment.getExternalStorageState().equals(
      android.os.Environment.MEDIA_MOUNTED)) { 

     String l = getIntent().getStringExtra("LABEL"); 

     extStorageDirectory = Environment.getExternalStorageDirectory() 
       .toString(); 

     File cacheDirectory = new File(extStorageDirectory 
       + "/myCompany/myApp/cache/" + l);// Create a Folder object 
                  // for the parent 
                  // directory 
     cacheDirectory.mkdirs();// Have the object build the folder 
           // if needed. 

    } else if (android.os.Environment.getExternalStorageState().equals(
      android.os.Environment.MEDIA_MOUNTED_READ_ONLY)) { 

     Alerts.sdCardMissing(this); 
    } 

} 
+0

我面臨着同樣的問題。雖然我設置了自己的自定義路徑,但webview使用默認緩存路徑。這是Android 4.4.2,聯想S8-50LC機型。 – nagu 2015-09-24 11:12:26

回答