【问题标题】:Web view setAppCachePath not working androidWeb视图setAppCachePath不起作用android
【发布时间】:2015-09-18 05:54:38
【问题描述】:

我想设置 AppCachePath() 路径,以便稍后检查哪些 url 被缓存,哪些不是在加载它们之前。如果没有互联网并且 url 没有被缓存,我想跳过加载它。

 File dir = new File(Environment.getExternalStorageDirectory().getPath()+"/Android/data/" +
 if (!dir.exists()) {
            dir.mkdirs();
     }
        Log.i("cache",dir.getPath());

  wv.getSettings().setAppCachePath(dir.getPath());         

但是,缓存文件夹已创建并始终为空。我认为 webview 仅在其默认位置缓存 url。这怎么可能 解决

我尝试了http://www.devahead.com/blog/2012/01/saving-the-android-webview-cache-on-the-sd-card/ 解决方案,但这也不起作用

我经历过: Check if url is cached webview android

WebView Cache Data Directory?

Saving External Cache Files

How to move webView cache to SD?

Check if file already exists in webview cache android

【问题讨论】:

  • 如果这不起作用也许您可以覆盖WebViewClient.shouldOverrideLoadingUrl(),然后自己将内容缓存在后台线程中。
  • 你能分享一下我是如何缓存 url 的吗?

标签: java android caching webview


【解决方案1】:

如果该方法不起作用,这只是一种解决方法。

这是一个可能的解决方案:

WebView webview = new WebView(context);
webview.setWebViewClient(new WebViewClient(){

    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
         makeGetRequestAndCacheResult(String url);
         return false;
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多