-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathFileUtils.java
More file actions
209 lines (176 loc) · 8.48 KB
/
FileUtils.java
File metadata and controls
209 lines (176 loc) · 8.48 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
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
package org.andengine.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.os.Environment;
/**
* (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga Inc.
*
* @author Nicolas Gramlich
* @since 13:53:33 - 20.06.2010
*/
public class FileUtils {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Fields
// ===========================================================
// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
// ===========================================================
// Methods
// ===========================================================
public static void copyToExternalStorage(final Context pContext, final int pSourceResourceID, final String pFilename) throws FileNotFoundException {
FileUtils.copyToExternalStorage(pContext, pContext.getResources().openRawResource(pSourceResourceID), pFilename);
}
public static void copyToInternalStorage(final Context pContext, final int pSourceResourceID, final String pFilename) throws FileNotFoundException {
FileUtils.copyToInternalStorage(pContext, pContext.getResources().openRawResource(pSourceResourceID), pFilename);
}
public static void copyToExternalStorage(final Context pContext, final String pSourceAssetPath, final String pFilename) throws IOException {
FileUtils.copyToExternalStorage(pContext, pContext.getAssets().open(pSourceAssetPath), pFilename);
}
public static void copyToInternalStorage(final Context pContext, final String pSourceAssetPath, final String pFilename) throws IOException {
FileUtils.copyToInternalStorage(pContext, pContext.getAssets().open(pSourceAssetPath), pFilename);
}
private static void copyToInternalStorage(final Context pContext, final InputStream pInputStream, final String pFilename) throws FileNotFoundException {
StreamUtils.copyAndClose(pInputStream, new FileOutputStream(new File(pContext.getFilesDir(), pFilename)));
}
public static void copyToExternalStorage(final InputStream pInputStream, final String pFilePath) throws FileNotFoundException {
if (FileUtils.isExternalStorageWriteable()) {
final String absoluteFilePath = FileUtils.getAbsolutePathOnExternalStorage(pFilePath);
StreamUtils.copyAndClose(pInputStream, new FileOutputStream(absoluteFilePath));
} else {
throw new IllegalStateException("External Storage is not writeable.");
}
}
public static void copyToExternalStorage(final Context pContext, final InputStream pInputStream, final String pFilePath) throws FileNotFoundException {
if (FileUtils.isExternalStorageWriteable()) {
final String absoluteFilePath = FileUtils.getAbsolutePathOnExternalStorage(pContext, pFilePath);
StreamUtils.copyAndClose(pInputStream, new FileOutputStream(absoluteFilePath));
} else {
throw new IllegalStateException("External Storage is not writeable.");
}
}
public static boolean isFileExistingOnExternalStorage(final String pFilePath) {
if (FileUtils.isExternalStorageReadable()) {
final String absoluteFilePath = FileUtils.getAbsolutePathOnExternalStorage(pFilePath);
final File file = new File(absoluteFilePath);
return file.exists() && file.isFile();
} else {
throw new IllegalStateException("External Storage is not readable.");
}
}
public static boolean isFileExistingOnExternalStorage(final Context pContext, final String pFilePath) {
if (FileUtils.isExternalStorageReadable()) {
final String absoluteFilePath = FileUtils.getAbsolutePathOnExternalStorage(pContext, pFilePath);
final File file = new File(absoluteFilePath);
return file.exists() && file.isFile();
} else {
throw new IllegalStateException("External Storage is not readable.");
}
}
public static boolean isDirectoryExistingOnExternalStorage(final Context pContext, final String pDirectory) {
if (FileUtils.isExternalStorageReadable()) {
final String absoluteFilePath = FileUtils.getAbsolutePathOnExternalStorage(pContext, pDirectory);
final File file = new File(absoluteFilePath);
return file.exists() && file.isDirectory();
} else {
throw new IllegalStateException("External Storage is not readable.");
}
}
public static boolean ensureDirectoriesExistOnExternalStorage(final Context pContext, final String pDirectory) {
if (FileUtils.isDirectoryExistingOnExternalStorage(pContext, pDirectory)) {
return true;
}
if (FileUtils.isExternalStorageWriteable()) {
final String absoluteDirectoryPath = FileUtils.getAbsolutePathOnExternalStorage(pContext, pDirectory);
return new File(absoluteDirectoryPath).mkdirs();
} else {
throw new IllegalStateException("External Storage is not writeable.");
}
}
public static InputStream openOnExternalStorage(final String pFilePath) throws FileNotFoundException {
final String absoluteFilePath = FileUtils.getAbsolutePathOnExternalStorage(pFilePath);
return new FileInputStream(absoluteFilePath);
}
public static InputStream openOnExternalStorage(final Context pContext, final String pFilePath) throws FileNotFoundException {
final String absoluteFilePath = FileUtils.getAbsolutePathOnExternalStorage(pContext, pFilePath);
return new FileInputStream(absoluteFilePath);
}
public static String[] getDirectoryListOnExternalStorage(final Context pContext, final String pFilePath) {
final String absoluteFilePath = FileUtils.getAbsolutePathOnExternalStorage(pContext, pFilePath);
return new File(absoluteFilePath).list();
}
public static String[] getDirectoryListOnExternalStorage(final Context pContext, final String pFilePath, final FilenameFilter pFilenameFilter) {
final String absoluteFilePath = FileUtils.getAbsolutePathOnExternalStorage(pContext, pFilePath);
return new File(absoluteFilePath).list(pFilenameFilter);
}
public static String getAbsolutePathOnInternalStorage(final Context pContext, final String pFilePath) {
return pContext.getFilesDir().getAbsolutePath() + "/" + pFilePath;
}
public static String getAbsolutePathOnExternalStorage(final String pFilePath) {
return Environment.getExternalStorageDirectory() + "/" + pFilePath;
}
public static String getAbsolutePathOnExternalStorage(final Context pContext, final String pFilePath) {
return Environment.getExternalStorageDirectory() + "/Android/data/" + pContext.getApplicationInfo().packageName + "/files/" + pFilePath;
}
public static boolean isExternalStorageWriteable() {
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}
public static boolean isExternalStorageReadable() {
final String state = Environment.getExternalStorageState();
return state.equals(Environment.MEDIA_MOUNTED) || state.equals(Environment.MEDIA_MOUNTED_READ_ONLY);
}
public static void copyFile(final File pSourceFile, final File pDestinationFile) throws IOException {
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(pSourceFile);
out = new FileOutputStream(pDestinationFile);
StreamUtils.copy(in, out);
} finally {
StreamUtils.close(in);
StreamUtils.close(out);
}
}
/**
* Deletes all files and sub-directories under <code>dir</code>. Returns
* true if all deletions were successful. If a deletion fails, the method
* stops attempting to delete and returns false.
*
* @param pFileOrDirectory
* @return
*/
public static boolean deleteDirectory(final File pFileOrDirectory) {
if(pFileOrDirectory.isDirectory()) {
final String[] children = pFileOrDirectory.list();
final int childrenCount = children.length;
for(int i = 0; i < childrenCount; i++) {
final boolean success = FileUtils.deleteDirectory(new File(pFileOrDirectory, children[i]));
if(!success) {
return false;
}
}
}
// The directory is now empty so delete it
return pFileOrDirectory.delete();
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}