J'utilise la bibliothèque Glide pour le chargement d'images. Il y a beaucoup d'images dans mon application, donc je veux effacer le cache une fois que la taille du cache est supérieure à 50 mb. Quelqu'un peut-il m'aider à le faire?Glisser, Effacer le cache lorsque la taille du cache est supérieure à 50 mb
3
A
Répondre
5
Appelez Glide.get (contexte) .clearDiskCache() en dehors du thread d'interface utilisateur. (Également tenir compte clearMemory() trop pour éviter des surprises après avoir effacé le cache disque) cela a fonctionné pour moi
new Thread(new Runnable() {
@Override
public void run() {
Glide.get(MainActivity.this).clearDiskCache();
}
}).start();
Il y a beaucoup d'images dans mon application et je veux effacer le cache une fois la taille du cache est supérieure à 50 Mo .
dans le cas où vous voulez mettre la limite de 50 Mo, vous pouvez mettre en œuvre le module de descente
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();
}
}
puis dans votre manifeste ajouter comme ça
<manifest
...
<application>
<meta-data
android:name="YourPackageNameHere.LimitCacheSizeGlideModule"
android:value="GlideModule" />
...
</application>
</manifest>
Comment j'obtiendrai connaître le cache actuel taille de sorte que je vais appeler la méthode ci-dessus une fois la taille du cache actuel va croiser 50 Mo? –
Une fois que j'ai défini la limite à 50 Mo, est-il nécessaire d'effacer le cache en utilisant Glide.get (MainActivity.this) .clearDiskCache(); ou la bibliothèque le fera automatiquement? –
no @SapnaSharma il va définir la limite que vous devez ne pas l'effacer manuellement, il sera fait automatiquement –