|
@@ -1,144 +0,0 @@
|
1
|
|
-package com.xhly.corelib.utils;
|
2
|
|
-
|
3
|
|
-import android.app.ActivityManager;
|
4
|
|
-import android.content.Context;
|
5
|
|
-import android.os.Environment;
|
6
|
|
-import android.os.StatFs;
|
7
|
|
-
|
8
|
|
-import java.io.BufferedReader;
|
9
|
|
-import java.io.File;
|
10
|
|
-import java.io.FileReader;
|
11
|
|
-import java.io.IOException;
|
12
|
|
-import java.text.DecimalFormat;
|
13
|
|
-
|
14
|
|
-public class FileSizeUtil {
|
15
|
|
-
|
16
|
|
- private static final int ERROR = -1;
|
17
|
|
- private static DecimalFormat fileIntegerFormat = new DecimalFormat("#0");
|
18
|
|
- private static DecimalFormat fileDecimalFormat = new DecimalFormat("#0.#");
|
19
|
|
-
|
20
|
|
- /**
|
21
|
|
- * SDCARD是否存
|
22
|
|
- */
|
23
|
|
- public static boolean externalMemoryAvailable() {
|
24
|
|
- return android.os.Environment.getExternalStorageState().equals(
|
25
|
|
- android.os.Environment.MEDIA_MOUNTED);
|
26
|
|
- }
|
27
|
|
-
|
28
|
|
- /**
|
29
|
|
- * 获取手机内部剩余存储空间
|
30
|
|
- *
|
31
|
|
- * @return
|
32
|
|
- */
|
33
|
|
- public static long getAvailableInternalMemorySize() {
|
34
|
|
- File path = Environment.getDataDirectory();
|
35
|
|
- StatFs stat = new StatFs(path.getPath());
|
36
|
|
- long blockSize = stat.getBlockSize();
|
37
|
|
- long availableBlocks = stat.getAvailableBlocks();
|
38
|
|
- return availableBlocks * blockSize;
|
39
|
|
- }
|
40
|
|
-
|
41
|
|
- /**
|
42
|
|
- * 获取手机内部总的存储空间
|
43
|
|
- *
|
44
|
|
- * @return
|
45
|
|
- */
|
46
|
|
- public static long getTotalInternalMemorySize() {
|
47
|
|
- File path = Environment.getDataDirectory();
|
48
|
|
- StatFs stat = new StatFs(path.getPath());
|
49
|
|
- long blockSize = stat.getBlockSizeLong();
|
50
|
|
- long totalBlocks = stat.getBlockCountLong();
|
51
|
|
- return totalBlocks * blockSize;
|
52
|
|
- }
|
53
|
|
-
|
54
|
|
- /**
|
55
|
|
- * 获取SDCARD剩余存储空间
|
56
|
|
- *
|
57
|
|
- * @return
|
58
|
|
- */
|
59
|
|
- public static long getAvailableExternalMemorySize() {
|
60
|
|
- if (externalMemoryAvailable()) {
|
61
|
|
- File path = Environment.getExternalStorageDirectory();
|
62
|
|
- StatFs stat = new StatFs(path.getPath());
|
63
|
|
- long blockSize = stat.getBlockSize();
|
64
|
|
- long availableBlocks = stat.getAvailableBlocks();
|
65
|
|
- return availableBlocks * blockSize;
|
66
|
|
- } else {
|
67
|
|
- return ERROR;
|
68
|
|
- }
|
69
|
|
- }
|
70
|
|
-
|
71
|
|
- /**
|
72
|
|
- * 获取SDCARD总的存储空间
|
73
|
|
- *
|
74
|
|
- * @return
|
75
|
|
- */
|
76
|
|
- public static long getTotalExternalMemorySize() {
|
77
|
|
- if (externalMemoryAvailable()) {
|
78
|
|
- File path = Environment.getExternalStorageDirectory();
|
79
|
|
- StatFs stat = new StatFs(path.getPath());
|
80
|
|
- long blockSize = stat.getBlockSize();
|
81
|
|
- long totalBlocks = stat.getBlockCount();
|
82
|
|
- return totalBlocks * blockSize;
|
83
|
|
- } else {
|
84
|
|
- return ERROR;
|
85
|
|
- }
|
86
|
|
- }
|
87
|
|
-
|
88
|
|
- /**
|
89
|
|
- * 获取系统总内存
|
90
|
|
- *
|
91
|
|
- * @param context 可传入应用程序上下文。
|
92
|
|
- * @return 总内存大单位为B。
|
93
|
|
- */
|
94
|
|
- public static long getTotalMemorySize(Context context) {
|
95
|
|
- String dir = "/proc/meminfo";
|
96
|
|
- try {
|
97
|
|
- FileReader fr = new FileReader(dir);
|
98
|
|
- BufferedReader br = new BufferedReader(fr, 2048);
|
99
|
|
- String memoryLine = br.readLine();
|
100
|
|
- String subMemoryLine = memoryLine.substring(memoryLine.indexOf("MemTotal:"));
|
101
|
|
- br.close();
|
102
|
|
- return Integer.parseInt(subMemoryLine.replaceAll("\\D+", "")) * 1024;
|
103
|
|
- } catch (IOException e) {
|
104
|
|
- e.printStackTrace();
|
105
|
|
- }
|
106
|
|
- return 0;
|
107
|
|
- }
|
108
|
|
-
|
109
|
|
- /**
|
110
|
|
- * 获取当前可用内存,返回数据以字节为单位。
|
111
|
|
- *
|
112
|
|
- * @param context 可传入应用程序上下文。
|
113
|
|
- * @return 当前可用内存单位为B。
|
114
|
|
- */
|
115
|
|
- public static long getAvailableMemory(Context context) {
|
116
|
|
- ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
117
|
|
- ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
|
118
|
|
- am.getMemoryInfo(memoryInfo);
|
119
|
|
- return memoryInfo.availMem;
|
120
|
|
- }
|
121
|
|
-
|
122
|
|
- /**
|
123
|
|
- * 单位换算
|
124
|
|
- *
|
125
|
|
- * @param size 单位为B
|
126
|
|
- * @param isInteger 是否返回取整的单位
|
127
|
|
- * @return 转换后的单位
|
128
|
|
- */
|
129
|
|
- public static String formatFileSize(long size, boolean isInteger) {
|
130
|
|
- int unit=1000;
|
131
|
|
- DecimalFormat df = isInteger ? fileIntegerFormat : fileDecimalFormat;
|
132
|
|
- String fileSizeString = "0M";
|
133
|
|
- if (size < unit && size > 0) {
|
134
|
|
- fileSizeString = df.format((double) size) + "B";
|
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";
|
139
|
|
- } else {
|
140
|
|
- fileSizeString = df.format((double) size / (unit * unit * unit)) + "G";
|
141
|
|
- }
|
142
|
|
- return fileSizeString;
|
143
|
|
- }
|
144
|
|
-}
|