的WebView缓存数据目录?缓存、目录、数据、WebView

2023-09-07 08:36:12 作者:蛋堡

在我的code我必须设置为SD卡自定义缓存目录,我点了WebView.getSettings()。setAppCachePath(),这个路径,但操作系统将缓存的缺省高速缓存( /data/data/com.your.package.appname/cache)。正在创建自定义目录,正确的,它应该是,但数据并没有进入它。我使用的是自定义目录的原因是缓存的将是大的和暂时的,所以我需要用户能够删除特定的缓​​存。

我失去了一些东西呢?任何帮助,code,或建议将AP preciated一如既往。

感谢-你。

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

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

     / **
     *  - 设置的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
            +/ myCompany中/对myApp /缓存/+ 1);
    。mWebView.getSettings()setAllowFileAccess(真正的);
    mWebView.getSettings()setAppCacheEnabled(真)。
    。mWebView.getSettings()setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

    mWebView.loadUrl(URL);

    mWebView.setWebChromeClient(新WebChromeClient(){
        公共无效onProgressChanged(web视图来看,INT进度){
            activity.setProgress(进度* 100);
            pd.show();

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

        @覆盖
        公共无效onReachedMaxAppCacheSize(长spaceNeeded,
                长totalUsedQuota,WebStorage.QuotaUpdater quotaUpdater){
            quotaUpdater.updateQuota(spaceNeeded * 2);
        }

    });

    mWebView.setWebViewClient(新WebViewClient(){
        @覆盖
        公共无效onReceivedError(web视图来看,INT错误code,
                字符串描述,字符串failingUrl){
            //重定向
            如果(url.equals(failingUrl)){
                //是的error.html我们被重定向到的地方
                //服务器如果我们在线
                mWebView.loadUrl(HTTP:www.aero-technologies.us/error.html);
                返回;
            }否则,如果(url.equals(failingUrl)){//缓存失败 - 我们
                                                //脱机不必
                                               //版本显示
                //这个code删除丑Android的无法打开网页
                //并且只显示一个对话框,说明我们没有网络
                view.loadData(,text / html的,UTF-8);
                // TODO  - >的ShowDialog(DIALOG_NONETWORK);
            }
        }

        @覆盖
        公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
            view.loadUrl(URL);
            返回true;
        }
    });

公共无效CreateCacheDirectory(){
    如果(android.os.Environment.getExternalStorageState()。等于(
            android.os.Environment.MEDIA_MOUNTED)){

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

        extStorageDirectory = Environment.getExternalStorageDirectory()
                的ToString();

        文件cacheDirectory =新的文件(extStorageDirectory
                +/ myCompany中/对myApp /缓存/+ 1); //创建一个文件夹对象
                                                           //父
                                                          // 目录
        cacheDirectory.mkdirs(); //有对象建立的文件夹
                               // 如果需要的话。

    }否则,如果(android.os.Environment.getExternalStorageState()。等于(
            android.os.Environment.MEDIA_MOUNTED_READ_ONLY)){

        Alerts.sdCardMissing(本);
    }

}
 
大家有谁知道 Word的缓存文件夹在哪个位置

解决方案

从我可以告诉通过阅读WebSettings code,对WebSettings缓存配置仅仅是对HTML5的应用程序:

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

In my code I have a custom cache directory set to the sdCard and I point the 'WebView.getSettings().setAppCachePath("")' to this path but the OS sends the cache to the default cache (/data/data/com.your.package.appname/cache). The custom directory is being created correctly where it should be, but the data doesn't get into it. The reason I am using a custom directory is the cache's will be large and temporary so I need the user to be able to delete specific caches.

Am I missing something here? Any help, code, or suggestions will be appreciated as always.

Thank-you.

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

}

解决方案

From what I could tell by reading the WebSettings code, the cache configuration on WebSettings is only for HTML5 apps:

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