2017-03-15 207 views

回答

5

在UI线程外部调用Glide.get(context).clearDiskCache()。 (还要考虑clearMemory()也防止意外清除磁盘高速缓存后),这个工作对我来说

new Thread(new Runnable() { 
      @Override 
      public void run() { 
      Glide.get(MainActivity.this).clearDiskCache(); 
      } 
    }).start(); 

有很多我的应用程序的图像,所以我要清除高速缓存,一旦高速缓存大小大于50 MB的。

的情况下,你想放的限制50 MB可以实现滑行模块

import android.annotation.TargetApi; 
import android.content.Context; 
import android.os.Build; 
import android.os.Environment; 
import android.os.StatFs; 
import android.util.Log; 

import com.bumptech.glide.Glide; 
import com.bumptech.glide.GlideBuilder; 
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory; 
import com.bumptech.glide.module.GlideModule; 
import com.example.MyApplication; 

import java.util.Locale; 

public class LimitCacheSizeGlideModule implements GlideModule { 
    // Modern device should have 8GB (=7.45GiB) or more! 
    private static final int SMALL_INTERNAL_STORAGE_THRESHOLD_GIB = 6; 
    private static final int DISK_CACHE_SIZE_FOR_SMALL_INTERNAL_STORAGE_MIB = 50*1024*1024; 

    @Override 
    public void applyOptions(Context context, GlideBuilder builder) { 
     if (MyApplication.from(context).isTest()) return; // NOTE: StatFs will crash on robolectric. 

     double totalGiB = getTotalBytesOfInternalStorage()/1024.0/1024.0/1024.0; 
     Log.i(String.format(Locale.US, "Internal Storage Size: %.1fGiB", totalGiB)); 
     if (totalGiB < SMALL_INTERNAL_STORAGE_THRESHOLD_GIB) { 
      Log.i("Limiting image cache size to " + DISK_CACHE_SIZE_FOR_SMALL_INTERNAL_STORAGE_MIB + "MiB"); 
      builder.setDiskCache(new InternalCacheDiskCacheFactory(context, DISK_CACHE_SIZE_FOR_SMALL_INTERNAL_STORAGE_MIB)); 
     } 
    } 

    @Override 
    public void registerComponents(Context context, Glide glide) { 
    } 

    private long getTotalBytesOfInternalStorage() { 
     // http://stackoverflow.com/a/4595449/1474113 
     StatFs stat = new StatFs(Environment.getDataDirectory().getPath()); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { 
      return getTotalBytesOfInternalStorageWithStatFs(stat); 
     } else { 
      return getTotalBytesOfInternalStorageWithStatFsPreJBMR2(stat); 
     } 
    } 

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) 
    private long getTotalBytesOfInternalStorageWithStatFs(StatFs stat) { 
     return stat.getTotalBytes(); 
    } 

    @SuppressWarnings("deprecation") 
    private long getTotalBytesOfInternalStorageWithStatFsPreJBMR2(StatFs stat) { 
     return (long) stat.getBlockSize() * stat.getBlockCount(); 
    } 
} 

,然后在清单中添加像这样

<manifest 

    ... 

    <application> 

     <meta-data 
      android:name="YourPackageNameHere.LimitCacheSizeGlideModule" 
      android:value="GlideModule" /> 

     ... 

    </application> 
</manifest>  
+0

我怎么会得到了解当前缓存大小,以便当前缓存大小将跨越50 MB时,我将调用上述方法? –

+0

一旦我将限制设置为50 MB,是否需要使用Glide.get(MainActivity.this).clearDiskCache()清除缓存。或库会自动做到这一点? –

+0

否@SapnaSharma它会设置你不需要手动清除的限制,它会自动完成 –