fix: 图片缓存清除
This commit is contained in:
parent
8418d6490d
commit
ecaf79afc5
|
|
@ -471,7 +471,7 @@ class TranslucentThemeActivity : BaseActivity(), View.OnClickListener, OnSeekBar
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun finish() {
|
override fun finish() {
|
||||||
GlideCacheUtil.getInstance().clearImageMemoryCache(this)
|
ImageCacheUtil.clearImageMemoryCache(this)
|
||||||
super.finish()
|
super.finish()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -212,11 +212,11 @@ class PreferencesFragment : PreferencesFragment() {
|
||||||
val clearCache = findPreference<Preference>("clear_cache")
|
val clearCache = findPreference<Preference>("clear_cache")
|
||||||
clearCache!!.summary = attachContext.getString(
|
clearCache!!.summary = attachContext.getString(
|
||||||
R.string.tip_cache,
|
R.string.tip_cache,
|
||||||
GlideCacheUtil.getInstance().getCacheSize(attachContext)
|
ImageCacheUtil.getCacheSize(attachContext)
|
||||||
)
|
)
|
||||||
clearCache.onPreferenceClickListener =
|
clearCache.onPreferenceClickListener =
|
||||||
Preference.OnPreferenceClickListener { preference: Preference ->
|
Preference.OnPreferenceClickListener { preference: Preference ->
|
||||||
GlideCacheUtil.getInstance().clearImageAllCache(attachContext)
|
ImageCacheUtil.clearImageAllCache(attachContext)
|
||||||
if (view != null) Util.createSnackbar(
|
if (view != null) Util.createSnackbar(
|
||||||
requireView(),
|
requireView(),
|
||||||
R.string.toast_clear_picture_cache_success,
|
R.string.toast_clear_picture_cache_success,
|
||||||
|
|
|
||||||
|
|
@ -27,10 +27,11 @@ import com.huanchengfly.tieba.post.ui.common.prefs.widgets.TextPref
|
||||||
import com.huanchengfly.tieba.post.ui.page.destinations.AboutPageDestination
|
import com.huanchengfly.tieba.post.ui.page.destinations.AboutPageDestination
|
||||||
import com.huanchengfly.tieba.post.ui.page.settings.LeadingIcon
|
import com.huanchengfly.tieba.post.ui.page.settings.LeadingIcon
|
||||||
import com.huanchengfly.tieba.post.ui.widgets.compose.*
|
import com.huanchengfly.tieba.post.ui.widgets.compose.*
|
||||||
import com.huanchengfly.tieba.post.utils.GlideCacheUtil
|
import com.huanchengfly.tieba.post.utils.ImageCacheUtil
|
||||||
import com.ramcosta.composedestinations.annotation.Destination
|
import com.ramcosta.composedestinations.annotation.Destination
|
||||||
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
|
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlin.concurrent.thread
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterialApi::class)
|
@OptIn(ExperimentalMaterialApi::class)
|
||||||
@Destination
|
@Destination
|
||||||
|
|
@ -54,7 +55,9 @@ fun MoreSettingsPage(
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
var cacheSize by remember { mutableStateOf("0.0B") }
|
var cacheSize by remember { mutableStateOf("0.0B") }
|
||||||
LaunchedEffect(Unit) {
|
LaunchedEffect(Unit) {
|
||||||
cacheSize = GlideCacheUtil.getInstance().getCacheSize(context)
|
thread {
|
||||||
|
cacheSize = ImageCacheUtil.getCacheSize(context)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
PrefsScreen(
|
PrefsScreen(
|
||||||
dataStore = LocalContext.current.dataStore,
|
dataStore = LocalContext.current.dataStore,
|
||||||
|
|
@ -156,7 +159,7 @@ fun MoreSettingsPage(
|
||||||
title = stringResource(id = R.string.title_clear_picture_cache),
|
title = stringResource(id = R.string.title_clear_picture_cache),
|
||||||
onClick = {
|
onClick = {
|
||||||
coroutineScope.launch {
|
coroutineScope.launch {
|
||||||
GlideCacheUtil.getInstance().clearImageAllCache(context)
|
ImageCacheUtil.clearImageAllCache(context)
|
||||||
cacheSize = "0.0B"
|
cacheSize = "0.0B"
|
||||||
snackbarHostState.showSnackbar(context.getString(R.string.toast_clear_picture_cache_success))
|
snackbarHostState.showSnackbar(context.getString(R.string.toast_clear_picture_cache_success))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,172 +0,0 @@
|
||||||
package com.huanchengfly.tieba.post.utils;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.os.Looper;
|
|
||||||
import android.text.TextUtils;
|
|
||||||
|
|
||||||
import com.bumptech.glide.Glide;
|
|
||||||
import com.bumptech.glide.load.engine.cache.ExternalPreferredCacheDiskCacheFactory;
|
|
||||||
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Glide缓存工具类
|
|
||||||
* Created by Trojx on 2016/10/10 0010.
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class GlideCacheUtil {
|
|
||||||
private static GlideCacheUtil inst;
|
|
||||||
|
|
||||||
public static GlideCacheUtil getInstance() {
|
|
||||||
if (inst == null) {
|
|
||||||
inst = new GlideCacheUtil();
|
|
||||||
}
|
|
||||||
return inst;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 格式化单位
|
|
||||||
*
|
|
||||||
* @param size size
|
|
||||||
* @return size
|
|
||||||
*/
|
|
||||||
private static String getFormatSize(double size) {
|
|
||||||
|
|
||||||
double kiloByte = size / 1024;
|
|
||||||
if (kiloByte < 1) {
|
|
||||||
return size + "B";
|
|
||||||
}
|
|
||||||
|
|
||||||
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";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 清除图片磁盘缓存
|
|
||||||
*/
|
|
||||||
public void clearImageDiskCache(Context context) {
|
|
||||||
try {
|
|
||||||
if (Looper.myLooper() == Looper.getMainLooper()) {
|
|
||||||
new Thread(() -> Glide.get(context).clearDiskCache()).start();
|
|
||||||
} else {
|
|
||||||
Glide.get(context).clearDiskCache();
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 清除图片内存缓存
|
|
||||||
*/
|
|
||||||
public void clearImageMemoryCache(Context context) {
|
|
||||||
try {
|
|
||||||
if (Looper.myLooper() == Looper.getMainLooper()) { //只能在主线程执行
|
|
||||||
Glide.get(context).clearMemory();
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 清除图片所有缓存
|
|
||||||
*/
|
|
||||||
public void clearImageAllCache(Context context) {
|
|
||||||
clearImageDiskCache(context);
|
|
||||||
clearImageMemoryCache(context);
|
|
||||||
String imageExternalCacheDir = context.getExternalCacheDir() + File.separator + ExternalPreferredCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR;
|
|
||||||
deleteFolderFile(imageExternalCacheDir, false);
|
|
||||||
deleteFolderFile(context.getCacheDir() + File.separator + ".shareTemp", false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取Glide造成的缓存大小
|
|
||||||
*
|
|
||||||
* @return CacheSize
|
|
||||||
*/
|
|
||||||
public String getCacheSize(Context context) {
|
|
||||||
try {
|
|
||||||
double glideCacheSize = getFolderSize(new File(context.getCacheDir(), InternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR));
|
|
||||||
double shareCacheSize = getFolderSize(new File(context.getCacheDir(), ".shareTemp"));
|
|
||||||
return getFormatSize(glideCacheSize + shareCacheSize);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取指定文件夹内所有文件大小的和
|
|
||||||
*
|
|
||||||
* @param file file
|
|
||||||
* @return size
|
|
||||||
*/
|
|
||||||
private long getFolderSize(File file) {
|
|
||||||
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
|
|
||||||
*/
|
|
||||||
private 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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,181 @@
|
||||||
|
package com.huanchengfly.tieba.post.utils
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.os.Looper
|
||||||
|
import android.text.TextUtils
|
||||||
|
import com.bumptech.glide.Glide
|
||||||
|
import com.bumptech.glide.load.engine.cache.ExternalPreferredCacheDiskCacheFactory
|
||||||
|
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory
|
||||||
|
import com.github.panpf.sketch.sketch
|
||||||
|
import java.io.File
|
||||||
|
import java.math.BigDecimal
|
||||||
|
import kotlin.concurrent.thread
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Glide缓存工具类
|
||||||
|
* Created by Trojx on 2016/10/10 0010.
|
||||||
|
*/
|
||||||
|
object ImageCacheUtil {
|
||||||
|
/**
|
||||||
|
* 清除图片磁盘缓存
|
||||||
|
*/
|
||||||
|
fun clearImageDiskCache(context: Context) {
|
||||||
|
try {
|
||||||
|
if (Looper.myLooper() == Looper.getMainLooper()) {
|
||||||
|
thread {
|
||||||
|
Glide.get(context).clearDiskCache()
|
||||||
|
context.sketch
|
||||||
|
.downloadCache
|
||||||
|
.clear()
|
||||||
|
context.sketch
|
||||||
|
.resultCache
|
||||||
|
.clear()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Glide.get(context).clearDiskCache()
|
||||||
|
context.sketch
|
||||||
|
.downloadCache
|
||||||
|
.clear()
|
||||||
|
context.sketch
|
||||||
|
.resultCache
|
||||||
|
.clear()
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清除图片内存缓存
|
||||||
|
*/
|
||||||
|
fun clearImageMemoryCache(context: Context) {
|
||||||
|
try {
|
||||||
|
if (Looper.myLooper() == Looper.getMainLooper()) { //只能在主线程执行
|
||||||
|
Glide.get(context).clearMemory()
|
||||||
|
context.sketch
|
||||||
|
.memoryCache
|
||||||
|
.clear()
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清除图片所有缓存
|
||||||
|
*/
|
||||||
|
fun clearImageAllCache(context: Context) {
|
||||||
|
clearImageDiskCache(context)
|
||||||
|
clearImageMemoryCache(context)
|
||||||
|
val imageExternalCacheDir =
|
||||||
|
context.externalCacheDir.toString() + File.separator + ExternalPreferredCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR
|
||||||
|
deleteFolderFile(imageExternalCacheDir, false)
|
||||||
|
deleteFolderFile(context.cacheDir.toString() + File.separator + ".shareTemp", false)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取Glide造成的缓存大小
|
||||||
|
*
|
||||||
|
* @return CacheSize
|
||||||
|
*/
|
||||||
|
fun getCacheSize(context: Context): String {
|
||||||
|
try {
|
||||||
|
val glideCacheSize = getFolderSize(
|
||||||
|
File(
|
||||||
|
context.cacheDir,
|
||||||
|
InternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR
|
||||||
|
)
|
||||||
|
).toDouble()
|
||||||
|
val shareCacheSize = getFolderSize(File(context.cacheDir, ".shareTemp")).toDouble()
|
||||||
|
val sketchCacheSize = context.sketch.run { downloadCache.size + resultCache.size }
|
||||||
|
return getFormatSize(glideCacheSize + shareCacheSize + sketchCacheSize)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取指定文件夹内所有文件大小的和
|
||||||
|
*
|
||||||
|
* @param file file
|
||||||
|
* @return size
|
||||||
|
*/
|
||||||
|
private fun getFolderSize(file: File): Long {
|
||||||
|
var size: Long = 0
|
||||||
|
try {
|
||||||
|
val fileList = file.listFiles()
|
||||||
|
for (aFileList in fileList) {
|
||||||
|
size = if (aFileList.isDirectory) {
|
||||||
|
size + getFolderSize(aFileList)
|
||||||
|
} else {
|
||||||
|
size + aFileList.length()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
|
return size
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除指定目录下的文件,这里用于缓存的删除
|
||||||
|
*
|
||||||
|
* @param filePath filePath
|
||||||
|
* @param deleteThisPath deleteThisPath
|
||||||
|
*/
|
||||||
|
private fun deleteFolderFile(filePath: String, deleteThisPath: Boolean) {
|
||||||
|
if (!TextUtils.isEmpty(filePath)) {
|
||||||
|
try {
|
||||||
|
val file = File(filePath)
|
||||||
|
if (file.isDirectory) {
|
||||||
|
val files = file.listFiles()
|
||||||
|
for (file1 in files) {
|
||||||
|
deleteFolderFile(file1.absolutePath, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (deleteThisPath) {
|
||||||
|
if (!file.isDirectory) {
|
||||||
|
file.delete()
|
||||||
|
} else {
|
||||||
|
if (file.listFiles().size == 0) {
|
||||||
|
file.delete()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 格式化单位
|
||||||
|
*
|
||||||
|
* @param size size
|
||||||
|
* @return size
|
||||||
|
*/
|
||||||
|
fun getFormatSize(size: Double): String {
|
||||||
|
val kiloByte = size / 1024
|
||||||
|
if (kiloByte < 1) {
|
||||||
|
return size.toString() + "B"
|
||||||
|
}
|
||||||
|
val megaByte = kiloByte / 1024
|
||||||
|
if (megaByte < 1) {
|
||||||
|
val result1 = BigDecimal(java.lang.Double.toString(kiloByte))
|
||||||
|
return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB"
|
||||||
|
}
|
||||||
|
val gigaByte = megaByte / 1024
|
||||||
|
if (gigaByte < 1) {
|
||||||
|
val result2 = BigDecimal(java.lang.Double.toString(megaByte))
|
||||||
|
return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB"
|
||||||
|
}
|
||||||
|
val teraBytes = gigaByte / 1024
|
||||||
|
if (teraBytes < 1) {
|
||||||
|
val result3 = BigDecimal(java.lang.Double.toString(gigaByte))
|
||||||
|
return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB"
|
||||||
|
}
|
||||||
|
val result4 = BigDecimal(teraBytes)
|
||||||
|
return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB"
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue