-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.java
More file actions
120 lines (88 loc) · 2.89 KB
/
Copy pathUtils.java
File metadata and controls
120 lines (88 loc) · 2.89 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
package com.example.kongalong.ximalaya_mvp.imageLoad;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.support.annotation.RequiresApi;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* Created by kongalong on 2016/11/8.
*/
public class Utils {
//判断是否有SD卡
public static boolean isMounted(){
String state = Environment.getExternalStorageState();
return state.equals(Environment.MEDIA_MOUNTED);
}
//获取disk缓存文件夹
public static File getDiskCacheFile(Context context,String fileName){
String diskCachepath = null;
//判断是否挂载sd卡,没有缓存到内置存储
if(isMounted()){
diskCachepath = context.getExternalCacheDir().getAbsolutePath();
}else{
diskCachepath = context.getCacheDir().getAbsolutePath();
}
File file = new File(diskCachepath + File.separator + fileName);
if(!file.exists()){
file.mkdirs();
}
return file;
}
//获取disk可用大小
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public static long getAvailableSpace(File file){
if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.GINGERBREAD){
return file.getUsableSpace();
}
StatFs statFs = new StatFs(file.getPath());
return statFs.getBlockSizeLong() * statFs.getAvailableBlocksLong();
}
//把路径转换成md5作为key(文件的名字)
public static String getkeyFromUrl(String url){
String ret = null;
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
byte[] bytes = url.getBytes();
messageDigest.update(bytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(0XFF & bytes[i]);
if(hex.length() == 0){
sb.append('0');
}
sb.append(hex);
}
ret = sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
ret = String.valueOf(url.hashCode());
}
return ret;
}
//关流
public static void closeIs(InputStream is){
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//关流
public static void closeOs(OutputStream os){
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}