保存外部缓存文​​件 [英] Saving External Cache Files

查看:126
本文介绍了保存外部缓存文​​件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想缓存的web视图文件保存到SD卡上的目录。我使用HTML5和服务器上的缓存清单文件。我设置外部目录使用setAppCachePath方法的SD卡,但缓存加载到应用程序默认的数据/数据​​/对myApp /缓存/ webiewCache'目录,而不是我设置使用setAppCachePath方法的目录。存在外部目录,但该数据没有得到它。任何意见或建议?下面是我采取的方法。

日Thnx的帮助。

 公共类DocumentView延伸活动{
/ **
 *  - 第一次创建活动时调用。
 * ================================================
 ** /
@覆盖
公共无效的onCreate(最终捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    串T = getIntent()getStringExtra(TITLE)。
    activityTitle.setText(T);

    字符串L = getIntent()getStringExtra(标签)。
    CreateCacheDirectory();

    字符串LBL = getIntent()getStringExtra(TITLE)。
    PD = ProgressDialog.show(这一点,+ LBL,从服务器检索数据
            真正);

    //  - 设置的WebView ==========================================
    最终的字符串URL = getIntent()getStringExtra(URL); //获取网址
                                                            //从传递
                                                            // previous
                                                            // 活动
    mWebView =(web视图)findViewById(R.id.webView1);
    mWebView.getSettings()setDomStorageEnabled(真)。

    //设置缓存大小为8 MB默认情况下。应该是绰绰有余
    mWebView.getSettings()setAppCacheMaxSize(1024 * 1024 * 8)。
    //高速缓存的缺省位置
    mWebView.getSettings()。setAppCachePath(
            extStorageDirectory +/ MYCO /对myApp /缓存/+ 1);
    //缓存设置
    。mWebView.getSettings()setAllowFileAccess(真正的);
    mWebView.getSettings()setAppCacheEnabled(真)。
    mWebView.getSettings()
            .setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    //其它web视图设置
    mWebView.getSettings()setJavaScriptEnabled(真)。

    //检查,看看是否有保存状态
    如果(savedInstanceState!= NULL){
        mWebView =(web视图)findViewById(R.id.webView1);
        mWebView.restoreState(savedInstanceState);
    }其他{//如果不是这样,从目的束加载新的网址
        mWebView =(web视图)findViewById(R.id.webView1);
        mWebView.loadUrl(URL);
        //显示进度给用户
        mWebView.setWebChromeClient(新WebChromeClient(){
            公共无效onProgressChanged(web视图来看,INT进度){
                activity.setProgress(进度* 100);

                如果(进度== 100)
                    pd.dismiss();
            }

            @覆盖
            //告诉客户端应用程序缓存已超过其
            //最大尺寸
            公共无效onReachedMaxAppCacheSize(长spaceNeeded,
                    长totalUsedQuota,
                    WebStorage.QuotaUpdater quotaUpdater){
                quotaUpdater.updateQuota(spaceNeeded * 2);
            }

        });
        //设置将接受各种通知的WebViewClient和
        // 要求
        mWebView.setWebViewClient(新WebViewClient(){
            @覆盖
            公共无效onReceivedError(web视图来看,INT错误code,
                    字符串描述,字符串failingUrl){
                //重定向
                如果(url.equals(failingUrl)){
                    //是的error.html我们被重定向到的地方
                    //服务器如果我们在线
                    mWebView.loadUrl(HTTP:www.myCo /的error.html);
                    返回;
                }否则,如果(url.equals(failingUrl)){//缓存失败 - 
                                                        // 我们
                                                        //没有一个
                                                        //离线
                                                        //版本显示
                    //这个code去除难看的Andr​​oid的
                    //无法打开网页
                    //并且只显示一个对话框,说明我们没有网络
                    view.loadData(,text / html的,UTF-8);
                    // TODO  - >的ShowDialog(DIALOG_NONETWORK);
                }
            }

            //设置将接受各种通知的WebViewClient
            //和要求
            @覆盖
            公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
                view.loadUrl(URL);
                返回true;
            }
        });
    }
    slidingDrawer(); //将slidingDrawer
    quickAction(); //将QuickAction栏界面
    tableOfContenants(); //将TOC按钮接口
}
 

解决方案

我试图解决同样的问题,在V11和多达我所用onLoadResource来保存的网址在SD卡上解决了这个,然后shouldOverrideUrlLoading(web视图看来,字符串URL)加载资源,如果我们有本地的资源。

在< V11似乎是一个没有战略,我可以找到,通过setAppCachePath设置缓存位置时,这是存储的地方,但是当CacheManager的是检查从缓存/加载资源它总是使用新的文件(context.getCacheDir (),webviewCache);这与亚历克斯克拉默斯博客链接

要在任何解决方案

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
}

解决方案

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

这篇关于保存外部缓存文​​件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆