保存外部缓存文​​件

发布于 2024-11-16 11:35:14 字数 4717 浏览 0 评论 0 原文

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

感谢您的帮助。

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
}

I am trying to save cached webView files to a directory on the SD-card. I am using HTML5 and a cache-manifest file on the server. I set an external directory to the sd card using the setAppCachePath method but the cache is loading to the apps default 'data/data/myApp/cache/webiewCache' directory and not the directory I set using the setAppCachePath method. The external directory exists but the data is not getting to it. Any ideas or suggestions? Below is the methods I am implementing.

Thnx for the help.

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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扎心 2024-11-23 11:35:14

我试图解决同样的问题,在 v11 及更高版本中,我通过使用 onLoadResource 将 url 保存在 SD 卡上,然后使用 shouldOverrideUrlLoading(WebView view, String url) 来加​​载资源(如果我们本地有资源)解决了这个问题。

链接一致!

任何解决方案

I trying to solve the same problem, in v11 and up I've solved this by used onLoadResource to saved the url on the SD card and then shouldOverrideUrlLoading(WebView view, String url) to load the resource if we have the resource locally.

On <v11 there seems to be a no strategy I can find, when setting the cache location by setAppCachePath this is storage somewhere, but when the CacheManager is checking/loading resource from cached it always uses new File(context.getCacheDir(), "webviewCache"); which is consistent with Alex Kramers blog a link!.

Any solutions to

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文