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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class FileUtils {
private String TAG = FileUtils.class.getSimpleName();
public static String SDPATH = Environment.getExternalStorageDirectory() + "/.photocomb/";
public static String DBName = "privalbum.db";
public static String DBPATH = "/data/data/com.cm.photocomb/databases/" + DBName;
/**
* 操作成功返回值
*/
public static final int SUCCESS = 0;
/**
* 操作失败返回值
*/
public static final int FAILED = -1;
private static final int BUF_SIZE = 32 * 1024; // 32KB
/**
* 获取录音文件路径
*/
public static String getFileName() {
String path = SDPATH;
File file = new File(path);
file.mkdirs();
path += System.currentTimeMillis();
return path;
}
/**
* Bitmap转字节数组
*/
public static byte[] Bitmap2Bytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
/**
* bytes转十六进制字符串
*/
public static String byte2HexStr(byte[] b) {
String stmp = "";
StringBuilder sb = new StringBuilder("");
for (int n = 0; n < b.length; n++) {
stmp = Integer.toHexString(b[n] & 0xFF);
sb.append((stmp.length() == 1) ? "0" + stmp : stmp);
sb.append(" ");
}
return sb.toString().toUpperCase().trim();
}
/**
* 十六进制字符串转byte数组
*/
public static byte[] hexStr2Bytes(String src) {
int m = 0, n = 0;
int l = src.length() / 2;
byte[] ret = new byte[l];
for (int i = 0; i < l; i++) {
m = i * 2 + 1;
n = m + 1;
ret[i] = Byte.decode("0x" + src.substring(i * 2, m) + src.substring(m, n));
}
return ret;
}
public static String bytesToHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
sb.append('0');
}
sb.append(hex);
}
return sb.toString();
}
private static final char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
public static String toHexString(byte[] b) {
StringBuilder sb = new StringBuilder(b.length * 2);
for (int i = 0; i < b.length; i++) {
sb.append(HEX_DIGITS[(b[i] & 0xf0) >>> 4]);
sb.append(HEX_DIGITS[b[i] & 0x0f]);
}
return sb.toString();
}
/**
* 计算文件MD5值
*/
public static String md5sum(String filename) {
if (TextUtils.isEmpty(filename)) {
return null;
}
InputStream fis;
byte[] buffer = new byte[1024];
int numRead = 0;
MessageDigest md5;
try {
fis = new FileInputStream(filename);
md5 = MessageDigest.getInstance("MD5");
while ((numRead = fis.read(buffer)) > 0) {
md5.update(buffer, 0, numRead);
}
fis.close();
return toHexString(md5.digest());
} catch (Exception e) {
return null;
}
}
/**
* 保存Bitmap到文件
*/
public static void saveBitmap(Bitmap bm, String picName) {
Log.e("", "保存图片");
try {
File f = new File(picName);
if (f.exists()) {
f.delete();
}
FileOutputStream out = new FileOutputStream(f);
if (bm != null) {
bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
}
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean saveBitmap(Bitmap bitmap, File file) {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
if (file.exists()) {
file.delete();
}
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 创建SD卡目录
*/
public static File createSDDir(String dirName) throws IOException {
File dir = new File(SDPATH + dirName);
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
dir.mkdir();
}
return dir;
}
/**
* 判断文件是否存在
*/
public static boolean isFileExist(String fileName) {
File file = new File(SDPATH + fileName);
return file.exists();
}
/**
* 删除文件
*/
public static void delFile(String fileName) {
File file = new File(SDPATH + fileName);
if (file.isFile()) {
file.delete();
}
}
/**
* 删除目录及子文件
*/
public static void deleteDir() {
File dir = new File(SDPATH);
if (dir == null || !dir.exists() || !dir.isDirectory())
return;
for (File file : dir.listFiles()) {
if (file.isFile())
file.delete();
else if (file.isDirectory())
deleteDir();
}
dir.delete();
}
public static boolean fileIsExists(String path) {
try {
File f = new File(path);
return f.exists();
} catch (Exception e) {
return false;
}
}
/**
* 删除单个文件
*/
public static boolean deleteFile(String sPath) {
File file = new File(sPath);
if (file.isFile() && file.exists()) {
file.delete();
return true;
}
return false;
}
/**
* 删除目录及目录下所有文件
*/
public static boolean deleteDirectory(String sPath) {
if (TextUtils.isEmpty(sPath)) {
return false;
}
if (!sPath.endsWith(File.separator)) {
sPath = sPath + File.separator;
}
File dirFile = new File(sPath);
if (!dirFile.exists() || !dirFile.isDirectory()) {
return false;
}
boolean flag = true;
File[] files = dirFile.listFiles();
for (File file : files) {
if (file.isFile()) {
flag = deleteFile(file.getAbsolutePath());
if (!flag) break;
} else {
flag = deleteDirectory(file.getAbsolutePath());
if (!flag) break;
}
}
if (!flag) return false;
return dirFile.delete();
}
/**
* 文件转字节数组
*/
public static byte[] getBytesFromFile(File f) {
if (f == null) {
return null;
}
try {
FileInputStream stream = new FileInputStream(f);
ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = stream.read(b)) != -1) {
out.write(b, 0, n);
}
stream.close();
out.close();
return out.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 字节数组保存为文件
*/
public static File getFileFromBytes(byte[] b, String outputFile) {
BufferedOutputStream stream = null;
File file = null;
try {
file = new File(outputFile);
FileOutputStream fstream = new FileOutputStream(file);
stream = new BufferedOutputStream(fstream);
stream.write(b);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return file;
}
/**
* 备份数据库
*/
public static void backupDB(Context mContext) {
try {
File backup = new File(SDPATH + DBName);
File dbFile = mContext.getDatabasePath(DBPATH);
backup.createNewFile();
fileCopy(dbFile, backup);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文件复制
*/
public static void fileCopy(File dbFile, File backup) throws IOException {
FileChannel inChannel = new FileInputStream(dbFile).getChannel();
FileChannel outChannel = new FileOutputStream(backup).getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inChannel != null) {
inChannel.close();
}
if (outChannel != null) {
outChannel.close();
}
}
}
/**
* assets文件复制到本地
*/
public static int assetToFile(Context context, String assetName, String path) {
return assetToFile(context, assetName, new File(path));
}
public static int assetToFile(Context context, String assetName, File file) {
InputStream is = null;
try {
is = context.getAssets().open(assetName);
return streamToFile(file, is, false);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (is != null) is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return FAILED;
}
/**
* 流写入文件
*/
public static int streamToFile(String path, InputStream is, boolean isAppend) {
return streamToFile(new File(path), is, isAppend);
}
public static int streamToFile(File file, InputStream is, boolean isAppend) {
checkParentPath(file);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file, isAppend);
byte[] buf = new byte[BUF_SIZE];
int readSize;
while ((readSize = is.read(buf)) != -1)
fos.write(buf, 0, readSize);
fos.flush();
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fos != null) fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return FAILED;
}
/**
* 检查并创建父目录
*/
public static void checkParentPath(String path) {
checkParentPath(new File(path));
}
public static void checkParentPath(File file) {
File parent = file.getParentFile();
if (parent != null && !parent.isDirectory())
createDir(parent);
}
/**
* 创建文件夹
*/
public static int createDir(String path) {
return createDir(new File(path));
}
public static int createDir(File file) {
if (file.exists()) {
if (file.isDirectory())
return SUCCESS;
file.delete();
}
if (file.mkdirs())
return SUCCESS;
return FAILED;
}
}
|