WebView缓存数据目录?

时间:2011-06-01 19:17:35

标签: android webview sd-card browser-cache

在我的代码中,我有一个自定义缓存目录设置为sdCard,我将'WebView.getSettings()。setAppCachePath(“”)指向此路径,但操作系统将缓存发送到默认缓存(/ data /数据/ com.your.package.appname /高速缓冲存储器)。正确创建自定义目录的位置,但数据不会进入。我使用自定义目录的原因是缓存将是大型和临时的,因此我需要用户能够删除特定的缓存。

我在这里遗漏了什么吗?任何帮助,代码或建议都将一如既往地受到赞赏。

感谢-你。

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);
    }

}

2 个答案:

答案 0 :(得分:2)

从我通过阅读WebSettings代码可以看出,WebSettings上的缓存配置仅适用于HTML5应用程序:

http://www.grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.3_r1/android/webkit/WebSettings.java#181

答案 1 :(得分:0)

这是Chromium bug 245549,已在2014年11月底修复,因此可能会影响6.0(API 23)以下的所有Android版本。