uniapp Android和iOS 獲取App緩存、清除緩存

1、獲取本機(jī)緩存
// 獲取緩存
       formatSize() {
            plus.cache.calculate((size) => {
                let sizeCache = parseInt(size);
                if (sizeCache == 0) {
                    this.fileSizeString = "0B";
                } else if (sizeCache < 1024) {
                    this.fileSizeString = sizeCache + "B";
                } else if (sizeCache < 1048576) {
                    this.fileSizeString = (sizeCache / 1024).toFixed(2) + "KB";
                } else if (sizeCache < 1073741824) {
                    this.fileSizeString = (sizeCache / 1048576).toFixed(2) + "MB";
                } else {
                    this.fileSizeString = (sizeCache / 1073741824).toFixed(2) + "GB";
                }
            });
        },
2、清除緩存
    clearStorage() {
            uni.showModal({
                title: '清除緩存',
                content: '您確定要清除緩存嗎?',
                success: (res) => {
                    if (res.confirm) {
                        console.log('用戶點(diǎn)擊確定');
                        this.clearCache();
                    } else if (res.cancel) {
                        console.log('用戶點(diǎn)擊取消');
                    }
                }
            });
        },
3、清理緩存
    clearCache() {
            let os = plus.os.name;
            if (os == 'Android') {
                let main = plus.android.runtimeMainActivity();
                let sdRoot = main.getCacheDir();
                let files = plus.android.invoke(sdRoot, "listFiles");
                let len = files.length;
                for (let i = 0; i < len; i++) {
                    let filePath = '' + files[i]; 
                    plus.io.resolveLocalFileSystemURL(filePath, (entry) => {
                        if (entry.isDirectory) {
                            entry.removeRecursively((entry)  => { 
                                uni.showToast({
                                    title: '緩存清理完成',
                                    duration: 2000
                                });
                                this.formatSize();  
                            }, (e) => {
                                console.log(e.message)
                            });
                        } else {
                            entry.remove();
                        }
                    }, (e) => {
                        console.log('文件路徑讀取失敗')
                    });
                }
            } else { // ios  
                plus.cache.clear(() =>{
                    uni.showToast({
                        title: '緩存清理完成',
                        duration: 2000
                    });
                    this.formatSize();
                });
            }
        }

本文只做記錄學(xué)習(xí)使用;原文:https://blog.csdn.net/weixin_63515766/article/details/133676748

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容