我看到一些应用程序,比如Dolphin浏览器(不是高清版,而是普通版),利用缓存到SD卡的方式来进行Webview,但我似乎无法弄清楚如何实现这一点,有人知道如何做或指导我吗?非常感谢任何帮助!谢谢 :)
这里是一篇文章,详细描述了如何将Webview缓存存储更改为使用SD卡:
http://www.devahead.com/blog/2012/01/saving-the-android-webview-cache-on-the-sd-card/我已经在我的应用程序中测试过,证明它能正常工作。
public class MainApplication extends Application {
// ...
@Override
public File getCacheDir() {
// NOTE: this method is used in Android 2.2 and higher
File cachePath = this.getExternalCachePath();
return cachePath != null ? cachePath : super.getCacheDir();
}
private File getExternalCachePath() {
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
File externalStorageDir = Environment.getExternalStorageDirectory();
// {SD_PATH}/Android/data/com.package.name/cache
File extStorageAppCachePath = new File(externalStorageDir, "Android" + File.separator + "data" + File.separator + this.getPackageName() + File.separator + "cache");
return extStorageAppCachePath;
}
return null;
}
}
public class SomeWebViewActivity extends Activity {
// ...
@Override
public File getCacheDir() {
// Android 2.1 and lower
return this.getApplicationContext().getCacheDir();
}
}
嗯,WebSettings
对象有许多 set...Path()
方法。不清楚它们中是否有任何一个是用于实际缓存的。还有一个 CacheManager
对象,它有一堆与缓存相关的静态方法,但没有明显的方法来更改缓存位置。
ws.setAppCachePath(cachedir)
存在,但它所做的只是创建一个空的 /cachedir/ApplicationCache.db
文件。我不知道如何解决这个问题。 - Luis A. Florit
Environment.getExternalStorageDirectory()
指向一个内部 SD 分区。是的,这并不是很合乎逻辑,但事实就是如此。 - Luis A. Florit