-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSDUtils.java
More file actions
125 lines (117 loc) · 3.72 KB
/
SDUtils.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
package momo.com.week12_project.utils;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Created by realmo on 2016/6/5 0005.
*/
public class SDUtils {
/**
* 保存文件到SD卡中
*
* @param in 网络文件输入流
* @param fileName 要保存的文件名
* @param filePath 文件保存在哪个目录,不包含SD卡跟路径
*/
public static void saveFile(InputStream in, String fileName, String filePath) throws Exception {
//先判断SD卡是否可用
if (!isMount()) {
Log.d("realmo", "================SD卡不可用===========");
return;
}
String path;
//获取SDcart路径
if (filePath == null) {
//保存到SDcard根目录
path = getSDcardPath();
} else {
path = getSDcardPath() + filePath;
}
//创建文件
File file = new File(path);
//如果目录文件不存在,则创建一个
if (!file.exists()) {
file.mkdirs();
} else {
//目录文件已经存在,但如果不是目录,则直接返回
if (!file.isDirectory()) {
Log.d("realmo", "============保存的路径不是有效的目录============");
return;
}
}
//创建文件
File newFile = new File(file, fileName);
OutputStream out = new FileOutputStream(newFile);
byte[] buffer = new byte[2048];
int tmp = 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
*/
public static String saveFile(byte[] arr, String fileName, String filePath) throws Exception {
//先判断SD卡是否可用
if (!isMount()) {
Log.d("realmo", "================SD卡不可用===========");
return null;
}
String path;
//获取SDcart路径
if (filePath == null) {
//保存到SDcard根目录
path = getSDcardPath();
} else {
path = getSDcardPath() + filePath;
}
//创建文件
File file = new File(path);
//如果目录文件不存在,则创建一个
if (!file.exists()) {
file.mkdir();
} else {
//目录文件已经存在,但如果不是目录,则直接返回
if (!file.isDirectory()) {
Log.d("realmo", "============保存的路径不是有效的目录============");
return null;
}
}
//创建文件
File newFile = new File(file, fileName);
String newPath = newFile.getAbsolutePath();
OutputStream out = new FileOutputStream(newFile);
out.write(arr, 0, arr.length);
out.flush();
out.close();
return newPath;
}
/**
* 判断SD卡是否挂载
*
* @return
*/
public static boolean isMount() {
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}
/**
* 获取SD卡路径,后面加上分隔符File.separator:/
*
* @return
*/
public static String getSDcardPath() {
return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
}
}