-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileUtil.java
More file actions
158 lines (135 loc) · 4.84 KB
/
FileUtil.java
File metadata and controls
158 lines (135 loc) · 4.84 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
import android.content.Context;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.util.Date;
/**
* Created by HUNGDH on 11/9/2016.
*/
public class FileUtils {
private static FileUtils instance = null;
private static Context mContext;
private static final String APP_DIR = "HUNGDH";
public static FileUtils getInstance(Context context) {
if (instance == null) {
synchronized (FileUtils.class) {
if (instance == null) {
mContext = context;
instance = new FileUtils();
}
}
}
return instance;
}
private FileUtils() {
if (isSDCanWrite()) {
creatSDDir(APP_DIR);
}
}
public boolean isSDCanWrite() {
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED)
&& Environment.getExternalStorageDirectory().canWrite()
&& Environment.getExternalStorageDirectory().canRead()) {
return true;
} else {
return false;
}
}
public File creatSDDir(String dirName) {
File dir = new File(getLocalPath() + dirName);
dir.mkdirs();
return dir;
}
private static String getLocalPath() {
String sdPath = null;
sdPath = Environment.getExternalStorageDirectory() + "/";
return sdPath;
}
public String getAppDirPath() {
String path = null;
if (getLocalPath() != null) {
path = getLocalPath() + APP_DIR + "/";
}
return path;
}
public File createTempFile(String prefix, String extension) throws IOException {
File file = new File(getAppDirPath() + "/" + prefix
+ System.currentTimeMillis() + extension);
file.createNewFile();
return file;
}
public File createFileWithName(String name, String extension) throws IOException {
name = name.trim().replaceAll(" ", "_").replaceAll("[-+.^:,]", "");
File file = new File(getAppDirPath() + "/" + name + extension);
if (file.exists()) {
file = new File(getAppDirPath() + "/" + name + "_" + System.currentTimeMillis() + extension);
} else {
file.createNewFile();
}
return file;
}
public static boolean deleteFile(String path) {
File file = new File(path);
return file.delete();
}
public File[] getListFiles() {
String path = Environment.getExternalStorageDirectory() + File.separator + APP_DIR;
File f = new File(path);
File[] files = f.listFiles();
return files;
}
public static String getFileName(String fullName) {
return fullName.substring(0, fullName.lastIndexOf("."));
}
public static String getFileType(String fullName) {
return fullName.substring(fullName.lastIndexOf(".") + 1, fullName.length());
}
public static Date getFileLastModified(String pathFile) {
File file = new File(pathFile);
return new Date(file.lastModified());
}
public static String getLastModified(File file) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
Date date = new Date(file.lastModified());
return sdf.format(date);
}
public static String getFileSize(File file) {
String value = null;
long size = file.length() / 1024;//call function and convert bytes into Kb
if (size >= 1024)
value = size / 1024 + " MB";
else
value = size + " KB";
return value;
}
public static void scanMedia(Context context, String filePath) {
MediaScannerConnection.scanFile(
context, new String[]{filePath}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("myLog", "Finished scanning " + path + " New row: " + uri);
}
});
}
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} catch (Exception e) {
e.printStackTrace();
return contentUri.getPath();
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}