Bladeren bron

1.添加工具类

master
wangwanlei 10 maanden geleden
bovenliggende
commit
1dde05d89a

+ 8
- 7
corelib/src/main/java/com/xhly/corelib/utils/FileSizeUtil.java Bestand weergeven

@@ -99,7 +99,7 @@ public class FileSizeUtil {
99 99
             String memoryLine = br.readLine();
100 100
             String subMemoryLine = memoryLine.substring(memoryLine.indexOf("MemTotal:"));
101 101
             br.close();
102
-            return Integer.parseInt(subMemoryLine.replaceAll("\\D+", "")) * 1024l;
102
+            return Integer.parseInt(subMemoryLine.replaceAll("\\D+", "")) * 1024;
103 103
         } catch (IOException e) {
104 104
             e.printStackTrace();
105 105
         }
@@ -127,16 +127,17 @@ public class FileSizeUtil {
127 127
      * @return 转换后的单位
128 128
      */
129 129
     public static String formatFileSize(long size, boolean isInteger) {
130
+        int unit=1000;
130 131
         DecimalFormat df = isInteger ? fileIntegerFormat : fileDecimalFormat;
131 132
         String fileSizeString = "0M";
132
-        if (size < 1024 && size > 0) {
133
+        if (size < unit && size > 0) {
133 134
             fileSizeString = df.format((double) size) + "B";
134
-        } else if (size < 1024 * 1024) {
135
-            fileSizeString = df.format((double) size / 1024) + "K";
136
-        } else if (size < 1024 * 1024 * 1024) {
137
-            fileSizeString = df.format((double) size / (1024 * 1024)) + "M";
135
+        } else if (size < unit * unit) {
136
+            fileSizeString = df.format((double) size / unit) + "K";
137
+        } else if (size < unit * unit * unit) {
138
+            fileSizeString = df.format((double) size / (unit * unit)) + "M";
138 139
         } else {
139
-            fileSizeString = df.format((double) size / (1024 * 1024 * 1024)) + "G";
140
+            fileSizeString = df.format((double) size / (unit * unit * unit)) + "G";
140 141
         }
141 142
         return fileSizeString;
142 143
     }

+ 18
- 4
corelib/src/main/java/com/xhly/corelib/utils/FileSizeUtils.kt Bestand weergeven

@@ -1,17 +1,31 @@
1 1
 package com.xhly.corelib.utils
2 2
 
3
+import android.app.usage.StorageStatsManager
3 4
 import android.content.Context
4
-import android.os.Environment
5
-import android.os.StatFs
5
+import android.os.Build
6
+import android.os.storage.StorageManager
7
+import androidx.appcompat.app.AppCompatActivity
6 8
 
7 9
 class FileSizeUtils {
8 10
     companion object{
9 11
         fun getTotalSpace(context: Context): Long {
10
-            return context.filesDir.totalSpace
12
+            return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
13
+                val storageStatsManager =
14
+                    context.getSystemService(AppCompatActivity.STORAGE_STATS_SERVICE) as StorageStatsManager
15
+                storageStatsManager.getTotalBytes(StorageManager.UUID_DEFAULT)
16
+            }else{
17
+                context.filesDir.totalSpace
18
+            }
11 19
         }
12 20
 
13 21
         fun getFreeSpace(context: Context): Long {
14
-            return context.filesDir.freeSpace
22
+            return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
23
+                val storageStatsManager =
24
+                    context.getSystemService(AppCompatActivity.STORAGE_STATS_SERVICE) as StorageStatsManager
25
+                storageStatsManager.getFreeBytes(StorageManager.UUID_DEFAULT)
26
+            }else{
27
+                context.filesDir.freeSpace
28
+            }
15 29
         }
16 30
     }
17 31
 }

Laden…
Annuleren
Opslaan