316044749 7 лет назад
Родитель
Сommit
6021a2ff2b

+ 5 - 0
app/src/main/AndroidManifest.xml

@@ -30,6 +30,11 @@
         android:roundIcon="@mipmap/ic_launcher_round"
         android:supportsRtl="true"
         android:theme="@style/AppTheme">
+
+        <meta-data
+            android:name="com.ynstkz.shitu.android.exter.GlideCacheModule"
+            android:value="GlideModule" />
+
         <activity android:name=".activity.HomeActivity">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />

+ 63 - 4
app/src/main/java/com/ynstkz/shitu/android/activity/SettingActivity.java

@@ -1,6 +1,9 @@
 package com.ynstkz.shitu.android.activity;
 
+import android.annotation.SuppressLint;
 import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
 import android.support.annotation.Nullable;
 import android.support.v7.widget.SwitchCompat;
 import android.view.View;
@@ -10,6 +13,7 @@ import android.widget.TextView;
 
 import com.ynstkz.shitu.android.R;
 import com.ynstkz.shitu.android.base.TitleBarActivity;
+import com.ynstkz.shitu.android.utils.FileUtils;
 
 import butterknife.Bind;
 
@@ -23,8 +27,8 @@ public class SettingActivity extends TitleBarActivity {
     TextView tvTitle;
     @Bind(R.id.view_title)
     RelativeLayout viewTitle;
-    @Bind(R.id.tv_username)
-    TextView tvUsername;
+    @Bind(R.id.tv_cache_size)
+    TextView tvCacheSize;
     @Bind(R.id.ic_clean_go)
     ImageView icCleanGo;
     @Bind(R.id.rl_clean)
@@ -52,7 +56,7 @@ public class SettingActivity extends TitleBarActivity {
     }
 
     private void initData(){
-
+        getCacheSize();
     }
 
     private void setListener(){
@@ -60,9 +64,64 @@ public class SettingActivity extends TitleBarActivity {
         rlClean.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
-
+                cleanCache();
             }
         });
     }
 
+    /**
+     * 异步获取缓存数据大小
+     */
+    private void getCacheSize() {
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                String cacheSize = FileUtils.getFolderSize(FileUtils.getImageCachePath());
+                Message msg = new Message();
+                msg.what = 100;
+                Bundle data = new Bundle();
+                data.putString("cachesize", cacheSize);
+                msg.setData(data);
+                handler.sendMessage(msg);
+            }
+        }).start();
+    }
+
+    /**
+     * 清除缓存
+     */
+    private void cleanCache(){
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    FileUtils.deleteDir(FileUtils.getImageCachePath());
+                    Message msg = new Message();
+                    msg.what = 101;
+                    Bundle data = new Bundle();
+                    data.putBoolean("isCleanSuccess", true);
+                    msg.setData(data);
+                    handler.sendMessage(msg);
+                } catch (Exception e){
+                    e.printStackTrace();
+                }
+            }
+        }).start();
+    }
+
+    @SuppressLint("HandlerLeak")
+    Handler handler = new Handler(){
+        @Override
+        public void handleMessage(Message msg) {
+            switch (msg.what){
+                case 100:
+                    tvCacheSize.setText(msg.getData().getString("cachesize"));
+                    break;
+                case 101:
+                    showToast("缓存已清理");
+                    tvCacheSize.setText("0B");
+                    break;
+            }
+        }
+    };
 }

+ 2 - 0
app/src/main/java/com/ynstkz/shitu/android/application/STApplication.java

@@ -12,11 +12,13 @@ import com.common.library.okhttp.OkHttpUtils;
 public class STApplication extends Application{
 
     private static Context context;
+    public static STApplication instance;
 
     @Override
     public void onCreate() {
         super.onCreate();
         context = getApplicationContext();
+        instance = this;
         if(com.common.library.Constants.DEVELOP_MODE){
             OkHttpUtils.getInstance().debug("okhttp", true);
         }

+ 4 - 0
app/src/main/java/com/ynstkz/shitu/android/common/Constants.java

@@ -9,4 +9,8 @@ public class Constants {
     public final static String ST_SHAREPREFREENCE="stsharepref";//SharePreference名字
 
     public final static String CACHE_USER_KEY = "UserBean";
+
+    public final static int IMAGE_CACHE_SIZE = 20 * 1024 * 1024;
+
+    public final static String DOWNLOAD_IMAGE_NAME = "image";
 }

+ 30 - 0
app/src/main/java/com/ynstkz/shitu/android/exter/GlideCacheModule.java

@@ -0,0 +1,30 @@
+package com.ynstkz.shitu.android.exter;
+
+import android.content.Context;
+
+import com.bumptech.glide.Glide;
+import com.bumptech.glide.GlideBuilder;
+import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory;
+import com.bumptech.glide.module.GlideModule;
+import com.ynstkz.shitu.android.common.Constants;
+import com.ynstkz.shitu.android.utils.FileUtils;
+
+/**
+ * 作者:fuchangle on 2018/3/1 13:38
+ */
+
+public class GlideCacheModule implements GlideModule{
+
+    @Override
+    public void registerComponents(Context context, Glide glide) {
+
+    }
+
+    @Override
+    public void applyOptions(Context context, GlideBuilder builder) {
+        /**
+         * 设置GLIDE图片缓存路径
+         */
+        builder.setDiskCache(new DiskLruCacheFactory(FileUtils.getImageCachePath(), Constants.IMAGE_CACHE_SIZE));
+    }
+}

+ 173 - 0
app/src/main/java/com/ynstkz/shitu/android/utils/FileUtils.java

@@ -0,0 +1,173 @@
+package com.ynstkz.shitu.android.utils;
+
+import android.os.Environment;
+import android.util.Log;
+
+import com.ynstkz.shitu.android.application.STApplication;
+import com.ynstkz.shitu.android.common.Constants;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.text.DecimalFormat;
+
+/**
+ * 作者:fuchangle on 2018/3/1 13:42
+ */
+
+public class FileUtils {
+
+    /**
+     * 是否存在内存卡判断
+     *
+     * @return
+     */
+    public static boolean existSDcard() {
+        if (Environment.MEDIA_MOUNTED.equals(Environment
+                .getExternalStorageState())) {
+            return true;
+        } else
+            return false;
+    }
+
+    /**
+     * 获取APP的根目录
+     * @return
+     */
+    public static String getAppRootPath(){
+        String path = STApplication.instance.getApplicationContext().getExternalFilesDir(null)+ File.separator;
+        File file = new File(path);
+        if (!file.exists()) {
+            file.mkdirs();
+        }
+        return path;
+    }
+
+    /**
+     * 获取图片缓存路径
+     * @return
+     */
+    public static String getImageCachePath(){
+        if(existSDcard()){
+            String path = getAppRootPath()  + Constants.DOWNLOAD_IMAGE_NAME+ File.separator;
+            File file = new File(path);
+            if(!file.exists()){
+                file.mkdirs();
+            }
+            return path;
+        } else {
+            return "";
+        }
+    }
+
+    /**
+     * 获取缓存文件夹大小
+     */
+    public static String getFolderSize(String filePath){
+        File file=new File(filePath);
+        long blockSize=0;
+        try {
+            if(file.isDirectory()){
+                blockSize = getFileSizes(file);
+            } else {
+                blockSize = getFileSize(file);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return FormetFileSize(blockSize);
+    }
+
+    /**
+     * 获取指定文件大小
+     * @param file
+     * @return
+     * @throws Exception
+     */
+    @SuppressWarnings("resource")
+    private static long getFileSize(File file) throws Exception {
+        long size = 0;
+        if (file.exists()){
+            FileInputStream fis = null;
+            fis = new FileInputStream(file);
+            size = fis.available();
+            fis.close();
+        } else	{
+            file.createNewFile();
+        }
+        return size;
+    }
+
+    /**
+     * 获取指定文件夹大小
+     * @param f
+     * @return
+     * @throws Exception
+     */
+    private static long getFileSizes(File f) throws Exception {
+        long size = 0;
+        File flist[] = f.listFiles();
+        for (int i = 0; i < flist.length; i++){
+            if (flist[i].isDirectory()){
+                size = size + getFileSizes(flist[i]);
+            }
+            else{
+                size =size + getFileSize(flist[i]);
+            }
+        }
+        return size;
+    }
+
+    /**
+     * 大小单位换算
+     * @param fileS
+     * @return
+     */
+    private static String FormetFileSize(long fileS) {
+        DecimalFormat df = new DecimalFormat("#.00");
+        String fileSizeString = "";
+        String wrongSize="0B";
+        if(fileS==0){
+            return wrongSize;
+        }
+        if (fileS < 1024){
+            fileSizeString = df.format((double) fileS) + "B";
+        }
+        else if (fileS < 1048576){
+            fileSizeString = df.format((double) fileS / 1024) + "KB";
+        }
+        else if (fileS < 1073741824){
+            fileSizeString = df.format((double) fileS / 1048576) + "MB";
+        }
+        else{
+            fileSizeString = df.format((double) fileS / 1073741824) + "GB";
+        }
+        return fileSizeString;
+    }
+
+
+    /**
+     * 递归删除目录下的所有文件及子目录下所有文件
+     * @param dir 将要删除的文件目录
+     * @return boolean Returns "true" if all deletions were successful.
+     *                 If a deletion fails, the method stops attempting to
+     *                 delete and returns "false".
+     */
+    public static void deleteDir(String dir) {
+        File file = new File(dir);
+        if (file.exists()) {
+            if (file.isDirectory()) {
+                File[] files = file.listFiles();
+                for (File subFile : files) {
+                    if (subFile.isDirectory()) {
+                        deleteDir(subFile.getPath());
+                    } else {
+                        subFile.delete();
+                    }
+                }
+            }
+            if (!file.getName().equals("files")) {
+                file.delete();
+            }
+        }
+    }
+}

+ 1 - 1
app/src/main/res/layout/activity_setting.xml

@@ -20,7 +20,7 @@
             android:text="清空缓存"/>
 
         <TextView
-            android:id="@+id/tv_username"
+            android:id="@+id/tv_cache_size"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_centerVertical="true"