Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android 4.4中不存在WebView缓存_Java_Android_Caching_Webview - Fatal编程技术网

Java Android 4.4中不存在WebView缓存

Java Android 4.4中不存在WebView缓存,java,android,caching,webview,Java,Android,Caching,Webview,WebView似乎没有在Android 4.4中创建或使用缓存,因此它不必要地消耗了大量带宽 在4.2.2中运行此简单的测试应用程序将显示正确的结果: ======================================================== Dumping Webview cache at /data/data/com.example.user.myapplication/cache/webviewCacheChromium 0th file: f_000006

WebView似乎没有在Android 4.4中创建或使用缓存,因此它不必要地消耗了大量带宽

在4.2.2中运行此简单的测试应用程序将显示正确的结果:

========================================================
Dumping Webview cache at /data/data/com.example.user.myapplication/cache/webviewCacheChromium
0th file: f_000006     33026
1th file: f_000005     40828
2th file: f_000004     37666
3th file: f_000003     76566
4th file: f_000002     66429
5th file: f_000001     20327
6th file: data_3     4202496
7th file: data_2     1056768
8th file: data_1     270336
9th file: data_0     45056
10th file: index     262512
Webview cache contains 11 files and takes up 5 Mb.
========================================================
但在4.4.4中,根本没有WebView缓存:

========================================================
Neither webviewCache nor webviewCacheChromium exists! Dumping App Cache instead.
Dumping App cache at /data/data/com.example.user.myapplication/cache
0th file: com.android.opengl.shaders_cache     10947
App cache contains 1 files and takes up 0 Mb.
========================================================
以下是我的源代码:

MainActivity.java:

package com.example.user.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    private WebView webview;
    private static final String TAG = "myapp";
    private static final String BAR = "========================================================";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webview = (WebView) findViewById(R.id.webview);

        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setDomStorageEnabled(true);
        webview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);

        webview.getSettings().setAppCacheEnabled(true);
        webview.getSettings().setAppCacheMaxSize(100 * 1000 * 1000);

        webview.setWebViewClient(new WebViewClient() {
        });
        webview.loadUrl("https://www.reddit.com/");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            String msg = dumpWebviewCache();
            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private String dumpWebviewCache() {
        Log.d(TAG, BAR);
        File appCache = getCacheDir();
        File webviewCache = new File(appCache, "webviewCache");
        File webviewCacheChromium = new File(appCache, "webviewCacheChromium");

        if (!webviewCache.exists() && !webviewCacheChromium.exists()) {
            Log.e(TAG, "Neither webviewCache nor webviewCacheChromium exists! Dumping App Cache instead.");
            return dumpDir("App cache", appCache);
        }
        if (!webviewCache.exists())
            webviewCache = webviewCacheChromium;

        return dumpDir("Webview cache", webviewCache);
    }

    private String dumpDir(String dirName, File dirFile) {
        Log.d(TAG, "Dumping " + dirName + " at " + dirFile.getAbsolutePath());
        int i = 0, totalSize = 0;
        for (File file : dirFile.listFiles()) {
            Log.d(TAG, String.format("%1$3s", i++) + "th file: " + file.getName() + "     " + file.length());
            totalSize += file.length();
        }
        String msg = dirName + " contains " + dirFile.listFiles().length + " files and takes up " + totalSize / (1024 * 1024) + " Mb.";
        Log.d(TAG, msg);
        Log.d(TAG, BAR);
        return msg;
    }
}
activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.user.myapplication.MainActivity">

    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</RelativeLayout>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
    <item android:id="@+id/action_settings" android:title="Dump WebView cache"
        android:orderInCategory="100" app:showAsAction="never" />
</menu>

strings.xml:

<resources>
    <string name="app_name">My Application</string>
</resources>

我的申请
AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.user.myapplication" >

    <uses-permission android:name="android.permission.INTERNET" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label=""
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

menu_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.user.myapplication.MainActivity">

    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</RelativeLayout>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
    <item android:id="@+id/action_settings" android:title="Dump WebView cache"
        android:orderInCategory="100" app:showAsAction="never" />
</menu>

是什么让您认为缓存目录必须被称为
webviewCache
webviewCache
?这是一个实现细节,您不应该依赖于此。在较新的版本中,缓存目录是在Java包名--
org.chromiu.android\u webview
之后调用的