forked from zilianliuxue/AndroidStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSdCardUtil.java
More file actions
203 lines (182 loc) · 6.23 KB
/
Copy pathSdCardUtil.java
File metadata and controls
203 lines (182 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
/**
* 基本功能:SD卡片工具
*/
public class SdCardUtil {
/**
* is sd card available.
* @return true if available
*/
public boolean isSdCardAvailable() {
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
}
/**
* Get {@link android.os.StatFs}.
*/
public static StatFs getStatFs(String path) {
return new StatFs(path);
}
/**
* Get phone data path.
*/
public static String getDataPath() {
return Environment.getDataDirectory().getPath();
}
/**
* Get SD card path.
*/
public static String getNormalSDCardPath() {
return Environment.getExternalStorageDirectory().getPath();
}
/**
* Get SD card path by CMD.
*/
public static String getSDCardPath() {
String cmd = "cat /proc/mounts";
String sdcard = null;
Runtime run = Runtime.getRuntime();// 返回与当前 Java 应用程序相关的运行时对象
BufferedReader bufferedReader = null;
try {
Process p = run.exec(cmd);// 启动另一个进程来执行命令
bufferedReader = new BufferedReader(new InputStreamReader(new BufferedInputStream(p.getInputStream())));
String lineStr;
while ((lineStr = bufferedReader.readLine()) != null) {
if (lineStr.contains("sdcard")
&& lineStr.contains(".android_secure")) {
String[] strArray = lineStr.split(" ");
if (strArray.length >= 5) {
sdcard = strArray[1].replace("/.android_secure", "");
return sdcard;
}
}
if (p.waitFor() != 0 && p.exitValue() == 1) {
// p.exitValue()==0表示正常结束,1:非正常结束
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
sdcard = Environment.getExternalStorageDirectory().getPath();
return sdcard;
}
/**
* Get SD card path list.
*/
public static ArrayList<String> getSDCardPathEx() {
ArrayList<String> list = new ArrayList<String>();
try {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("mount");
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
String line;
BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
if (line.contains("secure")) {
continue;
}
if (line.contains("asec")) {
continue;
}
if (line.contains("fat")) {
String columns[] = line.split(" ");
if (columns.length > 1) {
list.add("*" + columns[1]);
}
} else if (line.contains("fuse")) {
String columns[] = line.split(" ");
if (columns.length > 1) {
list.add(columns[1]);
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
/**
* Get available size of SD card.
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static long getAvailableSize(String path) {
try {
File base = new File(path);
StatFs stat = new StatFs(base.getPath());
return stat.getBlockSizeLong() * stat.getAvailableBlocksLong();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* Get SD card info detail.
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static SDCardInfo getSDCardInfo() {
SDCardInfo sd = new SDCardInfo();
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
sd.isExist = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
File sdcardDir = Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(sdcardDir.getPath());
sd.totalBlocks = sf.getBlockCountLong();
sd.blockByteSize = sf.getBlockSizeLong();
sd.availableBlocks = sf.getAvailableBlocksLong();
sd.availableBytes = sf.getAvailableBytes();
sd.freeBlocks = sf.getFreeBlocksLong();
sd.freeBytes = sf.getFreeBytes();
sd.totalBytes = sf.getTotalBytes();
}
}
return sd;
}
/**
* see more {@link android.os.StatFs}
*/
public static class SDCardInfo {
public boolean isExist;
public long totalBlocks;
public long freeBlocks;
public long availableBlocks;
public long blockByteSize;
public long totalBytes;
public long freeBytes;
public long availableBytes;
@Override
public String toString() {
return "SDCardInfo{" +
"isExist=" + isExist +
", totalBlocks=" + totalBlocks +
", freeBlocks=" + freeBlocks +
", availableBlocks=" + availableBlocks +
", blockByteSize=" + blockByteSize +
", totalBytes=" + totalBytes +
", freeBytes=" + freeBytes +
", availableBytes=" + availableBytes +
'}';
}
}
}