6

I've seen some apps like dolphin browser (not the HD version, the normal one) utilizing a cache-to-sd for webview but i can't seem to figure out how to do this, does anyone know how to do this or point me in the right direction? Any help is greatly appreciated! Thanks :)

Malcolm Lim
  • 135
  • 2
  • 7

2 Answers2

6

Here is the article which describes exactly how to change webview cache storage to use sd card: http://www.devahead.com/blog/2012/01/saving-the-android-webview-cache-on-the-sd-card/

I've already tested it in my application and it has proven to work.

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();
    }
}
vortexwolf
  • 13,967
  • 2
  • 54
  • 72
  • On some devices (e.g., Samsung), `Environment.getExternalStorageDirectory()` points to an INTERNAL SD partition. Yeah, not very logical, but it is. – Luis A. Florit Mar 27 '14 at 00:49
  • @LuisA.Florit Some people already asked questions how to get a path to the external storage on samsung devices, but answers are very hacky and may not work on some devices: http://stackoverflow.com/questions/5524105/how-could-i-get-the-correct-external-storage-on-samsung-and-all-other-devices – vortexwolf Mar 27 '14 at 17:33
1

Well, the WebSettings object has a number of set...Path() methods. It is unclear if any of them are for the actual cache. There is also the CacheManager object, which has a bunch of static methods related to the cache, but no obvious way to change the cache location.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I couldn't find any answers via a google search either, but i'm really wondering how others managed to successfully move their cache entirely from the internal memory device to the sd card. Do have any other ideas? – Malcolm Lim Aug 03 '10 at 02:14
  • 1
    Yes, `ws.setAppCachePath(cachedir)` exists, but all it does is to create an empty `/cachedir/ApplicationCache.db` file. I don't know how to solve this issue. – Luis A. Florit Mar 27 '14 at 00:51