1. 程式人生 > >Android Glide快取清除與獲取快取大小

Android Glide快取清除與獲取快取大小

不多說了,還是直接上程式碼吧

/**
 * Created by zhaoyong on 2016/6/21.
 * Glide快取工具類
 */
public class ImageCatchUtil {

    private static ImageCatchUtil inst;
    private final String ImageExternalCatchDir = Application.getInstance().getExternalCacheDir() + "/image_cache";

    public static ImageCatchUtil getInstance() {
        if
(inst == null) { inst = new ImageCatchUtil(); } return inst; } /** * 清除圖片磁碟快取 */ public void clearImageDiskCache() { try { if (Looper.myLooper() == Looper.getMainLooper()) { new Thread(new Runnable() { @Override
public void run() { Glide.get(Application.getInstance()).clearDiskCache(); } }).start(); } else { Glide.get(Application.getInstance()).clearDiskCache(); } } catch (Exception e) { e.printStackTrace(); } } /** * 清除圖片記憶體快取 */
public void clearImageMemoryCache() { try { if (Looper.myLooper() == Looper.getMainLooper()) { //只能在主執行緒執行 Glide.get(Application.getInstance()).clearMemory(); } } catch (Exception e) { e.printStackTrace(); } } /** * 清除圖片所有快取 */ public void clearImageAllCache() { clearImageDiskCache(); clearImageMemoryCache(); deleteFolderFile(ImageExternalCatchDir, true); } /** * 獲取Glide造成的快取大小 * * @return CacheSize */ public String getCacheSize() { try { return getFormatSize(getFolderSize(new File(Application.getInstance().getCacheDir() + "/image_cache"))); } catch (Exception e) { e.printStackTrace(); } return ""; } /** * 獲取指定資料夾內所有檔案大小的和 * * @param file file * @return size * @throws Exception */ public long getFolderSize(File file) throws Exception { long size = 0; try { File[] fileList = file.listFiles(); for (File aFileList : fileList) { if (aFileList.isDirectory()) { size = size + getFolderSize(aFileList); } else { size = size + aFileList.length(); } } } catch (Exception e) { e.printStackTrace(); } return size; } /** * 刪除指定目錄下的檔案,這裡用於快取的刪除 * * @param filePath filePath * @param deleteThisPath deleteThisPath */ public void deleteFolderFile(String filePath, boolean deleteThisPath) { if (!TextUtils.isEmpty(filePath)) { try { File file = new File(filePath); if (file.isDirectory()) { File files[] = file.listFiles(); for (File file1 : files) { deleteFolderFile(file1.getAbsolutePath(), true); } } if (deleteThisPath) { if (!file.isDirectory()) { file.delete(); } else { if (file.listFiles().length == 0) { file.delete(); } } } } catch (Exception e) { e.printStackTrace(); } } } /** * 格式化單位 * * @param size size * @return size */ public static String getFormatSize(double size) { double kiloByte = size / 1024; if (kiloByte < 1) { return size + "Byte"; } double megaByte = kiloByte / 1024; if (megaByte < 1) { BigDecimal result1 = new BigDecimal(Double.toString(kiloByte)); return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB"; } double gigaByte = megaByte / 1024; if (gigaByte < 1) { BigDecimal result2 = new BigDecimal(Double.toString(megaByte)); return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB"; } double teraBytes = gigaByte / 1024; if (teraBytes < 1) { BigDecimal result3 = new BigDecimal(Double.toString(gigaByte)); return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB"; } BigDecimal result4 = new BigDecimal(teraBytes); return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB"; } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165

GlideConfiguration配置檔案,指定快取目錄等方法都在這裡面

import android.content.Context;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool;
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.load.engine.cache.MemorySizeCalculator;
import com.bumptech.glide.module.GlideModule;

/**
 * Created by zhaoyong on 2016/6/16.
 * Glide配置檔案
 */
public class GlideConfiguration implements GlideModule {

    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        //自定義快取目錄,磁碟快取給150M
        builder.setDiskCache(new InternalCacheDiskCacheFactory(context, "image_catch", 150 * 1024 * 1024));
    }

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

    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

Manifest.xml

 <!--Glide配置檔案-->
<meta-data android:name="com.xxx.xxx.glide.config.GlideConfiguration" android:value="GlideModule" />
  • 1
  • 2
  • 1
  • 2

相關推薦

Android Glide快取清除獲取快取大小

不多說了,還是直接上程式碼吧 /** * Created by zhaoyong on 2016/6/21. * Glide快取工具類 */ public class ImageCatchUtil { private static ImageCatchUtil inst; priva

7.xamarin.android 發布簽名控制apk大小

其他 alt 等待 標識 執行 國內 ima 應用 需要 概述 做了xamarin android 後大家想打包一個apk,發布給其他人使用本章我們將帶領大家如何打包簽名一個apk。 打包 對於VS2017 或者是VS MAC來說打包一個APK非常簡單。 首選

瀏覽器快取cookie伺服器快取

之前對快取都是一知半解的,都是基於理論之上沒有正整實際動手驗證過,今天抽時間終於把快取重新理了一片 1、 設定js 、css緩存       http頭部中設定expires,Cache-Contro

Hibernate快取原理查詢快取的組合探究

來源:http://www.javaeye.com/topic/431603 0.前言          由於對Hibernate的二級快取和查詢快取的區別不瞭解,也不知道它們起什麼作用。於是動手做了一些實驗,對它們的組合使用有了一個表面的認識。 1.前提      

Android Glide獲取快取大小清除快取

GlideCatchSimple SimpleDemo請看github Glide快取Simple 快取路徑的指定 快取大小的獲取 磁碟快取清除(兩種方法) 記憶體快取清除 可clone之後檢視使用Simple Glide cache Si

Glide 快取工具例子,快取大小獲取,磁碟快取清除(2 種方法),記憶體快取清除

Glide 快取 Simple快取路徑的指定快取大小的獲取磁碟快取清除(兩種方法)記憶體快取清除可 clone 之後檢視使用 SimpleGlide cache Simple.The cache path specifiedThe cache sizeThe disk cache (two ways)Memo

Android記錄20-獲取快取大小清除快取功能

Android開發記錄20-獲取快取大小和清除快取功能轉載請註明:IT_xiao小巫 部落格地址:http://blog.csdn.net/wwj_748前言本篇部落格要給大家分享的如何獲取應用快取的大小和清除快取的功能,我們知道我們應用當中經常會產生一些資料,比如圖片的快取

iOS 獲取快取大小清除快取

首先要獲取cache資料夾路徑 #define cachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] //

iOS開發 SDWebImage 獲取快取清除快取

獲取快取:     //獲取快取圖片的大小(位元組)     NSUInteger bytesCache = [[SDImageCache sharedImageCache] getSize];     //換算成 MB (

android Glide 獲取磁碟快取

Glide是Google推薦的圖片載入庫, 載入圖片一般以下面的形式: Glide.with(context).load(ImgUrl) .asBitmap() .error(R.drawable.error) .pla

android獲取快取大小並清理快取

本文主要注重介紹實戰操作,理論知識可能介紹的不多,勿噴 簡單說兩句快取的好處: 採用快取,可以進一步大大緩解資料互動的壓力,又能提供一定的離線瀏覽。下邊我簡略列舉一下快取管理的適用環境: 1. 提供網路服務的應用 2. 資料更新不需要實時更新,哪怕是3-5分鐘的延遲

Android glide 圖片從快取

private class getImageCacheAsyncTask extends AsyncTask<String, Void, File> { private final Context context; public getImageCach

Android RxJava操作符的學習---組合合併操作符---從磁碟或記憶體快取獲取快取資料

1. 需求場景     2. 功能說明 對於從磁碟 / 記憶體快取中 獲取快取資料 的功能邏輯如下: 3. 具體實現 詳細請看程式碼註釋 // 該2變數用於模擬記憶體快取 & 磁碟快取中的資料 String me

Android快取策略圖片載入

Android快取策略 Android快取策略 LruCache DiskLruCache glide 鏈式呼叫 配合生命週期使用 快取設定 fresco 新增依賴

Android RxJava 實戰系列:從磁碟 / 記憶體快取獲取快取資料

前言 Rxjava,由於其基於事件流的鏈式呼叫、邏輯簡潔 & 使用簡單的特點,深受各大 Android開發者的歡迎。 RxJava如此受歡迎的原因,在於其提供了豐富 &

Android面試題(22)-lruCacheDiskLruCache快取詳解

關於lruCache(最近最少使用)的演算法,這是一個比較重要的演算法,它的應用非常廣泛,不僅僅在Android中使用,Linux系統等其他地方中也有使用;今天就來看一看這其中的奧祕;講到LruCache,就不得不講一講LinkedHashMap,而對於LinkedHashM

(微信小程式)關於require引入JS裡 wx.getStorageSync()無法即時獲取快取內容的分析記錄

先交代問題場景:       LZ要做一個小程式內資料切換功能,即在首頁做一個按鈕,點選了之後切換全部接口裡的一個請求引數值。(即將該值由A改為B)。  OK ,  很自然的想到了利用本地快取。      一切都是那麼的順利,將全域性的介面地址修改為從快取中獲取該值之後,

Android實現清除應用程式快取

我使用的是反射的方法來獲取某個應用程式的快取大小,但是沒能實現通過反射的方法來清除該應用快取,所以我只能呼叫系統的設定意圖來清除快取。而且在真機上沒什麼問題,模擬器上就有些問題了。 1.需要三個AIDL檔案 注意:在新增三個aidl檔案後一定要Rebuild Proje

Glide作為圖片快取,清除快取的合理方法

1.glide提供的方法: 下面是glide快取的配置 public class GlideCache implements GlideModule { @Override public void applyOptions(Context context, Glid

檔案系統快取裡記憶體頁的最小分配單元page sizeI/O大小的最佳匹配關係討論

     cache page size就是記憶體頁的最小分配單元,預設8KB,應用於R/W cache。如果I/O < 8KB,那麼一個page可能會服務多個I/O,從而優化了cache page的使用。 如果應用環境是Mixed I/O,建議不要更改。如果應用環境主要是【順序,大I/O】,將pa