- Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSDUtils.java
More file actions
Latest commit
125 lines (117 loc) · 3.72 KB
/
Copy pathSDUtils.java
File metadata and controls
125 lines (117 loc) · 3.72 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
packagemomo.com.week12_project.utils;
importandroid.os.Environment;
importandroid.util.Log;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.InputStream;
importjava.io.OutputStream;
/**
* Created by realmo on 2016/6/5 0005.
*/
publicclassSDUtils {
/**
* 保存文件到SD卡中
*
* @param in 网络文件输入流
* @param fileName 要保存的文件名
* @param filePath 文件保存在哪个目录,不包含SD卡跟路径
*/
publicstaticvoidsaveFile(InputStreamin, StringfileName, StringfilePath) throwsException {
//先判断SD卡是否可用
if (!isMount()) {
Log.d("realmo", "================SD卡不可用===========");
return;
}
Stringpath;
//获取SDcart路径
if (filePath == null) {
//保存到SDcard根目录
path = getSDcardPath();
} else {
path = getSDcardPath() + filePath;
}
//创建文件
Filefile = newFile(path);
//如果目录文件不存在,则创建一个
if (!file.exists()) {
file.mkdirs();
} else {
//目录文件已经存在,但如果不是目录,则直接返回
if (!file.isDirectory()) {
Log.d("realmo", "============保存的路径不是有效的目录============");
return;
}
}
//创建文件
FilenewFile = newFile(file, fileName);
OutputStreamout = newFileOutputStream(newFile);
byte[] buffer = newbyte[2048];
inttmp = 0;
while ((tmp = in.read(buffer)) != -1) {
out.write(buffer, 0, tmp);
}
out.flush();
out.close();
in.close();
}
/**
* 保存文件到SD卡中
*
* @param arr 保存的字节数组
* @param fileName 要保存的文件名
* @param filePath 文件保存在哪个目录,不包含SD卡跟路径
* @return 返回保存后的文件名及路径
* @throws Exception
*/
publicstaticStringsaveFile(byte[] arr, StringfileName, StringfilePath) throwsException {
//先判断SD卡是否可用
if (!isMount()) {
Log.d("realmo", "================SD卡不可用===========");
returnnull;
}
Stringpath;
//获取SDcart路径
if (filePath == null) {
//保存到SDcard根目录
path = getSDcardPath();
} else {
path = getSDcardPath() + filePath;
}
//创建文件
Filefile = newFile(path);
//如果目录文件不存在,则创建一个
if (!file.exists()) {
file.mkdir();
} else {
//目录文件已经存在,但如果不是目录,则直接返回
if (!file.isDirectory()) {
Log.d("realmo", "============保存的路径不是有效的目录============");
returnnull;
}
}
//创建文件
FilenewFile = newFile(file, fileName);
StringnewPath = newFile.getAbsolutePath();
OutputStreamout = newFileOutputStream(newFile);
out.write(arr, 0, arr.length);
out.flush();
out.close();
returnnewPath;
}
/**
* 判断SD卡是否挂载
*
* @return
*/
publicstaticbooleanisMount() {
returnEnvironment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}
/**
* 获取SD卡路径,后面加上分隔符File.separator:/
*
* @return
*/
publicstaticStringgetSDcardPath() {
returnEnvironment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
}
}