保存外部缓存文​​件

时间:2011-06-22 06:10:40

标签: android html5 caching webview

我正在尝试将缓存的webView文件保存到SD卡上的目录中。我在服务器上使用HTML5和缓存清单文件。我使用setAppCachePath方法将外部目录设置到sd卡,但缓存加载到apps默认的'data / data / myApp / cache / webiewCache'目录,而不是我使用setAppCachePath方法设置的目录。外部目录存在但数据未到达。任何想法或建议?以下是我正在实施的方法。

Thnx寻求帮助。

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

    String t = getIntent().getStringExtra("TITLE");
    activityTitle.setText(t);

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

    String lbl = getIntent().getStringExtra("TITLE");
    pd = ProgressDialog.show(this, "" + lbl, "Retrieving data from server",
            true);

    // -- 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 + "/myCo/myApp/cache/" + l);
    // Cache settings
    mWebView.getSettings().setAllowFileAccess(true);
    mWebView.getSettings().setAppCacheEnabled(true);
    mWebView.getSettings()
            .setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    // Misc webView settings
    mWebView.getSettings().setJavaScriptEnabled(true);

    // Check to see if there is a saved state
    if (savedInstanceState != null) {
        mWebView = (WebView) findViewById(R.id.webView1);
        mWebView.restoreState(savedInstanceState);
    } else { // If not, load the new URL from the intent bundle
        mWebView = (WebView) findViewById(R.id.webView1);
        mWebView.loadUrl(url);
        // Show the progress to the user
        mWebView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                activity.setProgress(progress * 100);

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

            @Override
            // Tell the client that the Application Cache has exceeded its
            // max size
            public void onReachedMaxAppCacheSize(long spaceNeeded,
                    long totalUsedQuota,
                    WebStorage.QuotaUpdater quotaUpdater) {
                quotaUpdater.updateQuota(spaceNeeded * 2);
            }

        });
        // Set the WebViewClient that will receive various notifications and
        // requests
        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.myCo/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);
                }
            }

            // Set the WebViewClient that will receive various notifications
            // and requests
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
    }
    slidingDrawer();// Load the slidingDrawer
    quickAction();// Load the QuickAction Bar interface
    tableOfContenants();// Load the TOC button interface
}

1 个答案:

答案 0 :(得分:1)

我试图解决同样的问题,在v11及以上我通过使用onLoadResource来保存SD卡上的url,然后如果我们有资源则应该加载资源(WebView视图,字符串url)来加载资源本地。

On< v11似乎没有我可以找到的策略,当通过setAppCachePath设置缓存位置时,这是存储在某处,但是当CacheManager从缓存中检查/加载资源时,它总是使用新的File(context.getCacheDir) (),“webviewCache”);这与Alex Kramers博客a link一致!

任何解决方案