Skip to content

Commit f235a48

Browse files
committed
Update FileUtil.java
1 parent e4315e4 commit f235a48

File tree

1 file changed

+83
-2
lines changed
  • toolsLibrary/src/main/java/com/bvtech/toolslibrary/Utility

1 file changed

+83
-2
lines changed

toolsLibrary/src/main/java/com/bvtech/toolslibrary/Utility/FileUtil.java

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package com.bvtech.toolslibrary.utility;
22

3+
import android.content.Context;
4+
import android.content.res.AssetManager;
35
import android.os.Environment;
46
import android.util.Log;
57

68
import java.io.File;
9+
import java.io.FileOutputStream;
10+
import java.io.IOException;
11+
import java.io.InputStream;
12+
import java.io.OutputStream;
713
import java.text.SimpleDateFormat;
814
import java.util.Date;
915
import java.util.Locale;
@@ -97,8 +103,11 @@ public static File createOutputVideoFile(String directoryName) {
97103

98104
public static File createOutputDocumentFile(String directoryName) {
99105
// External sdcard location
100-
File documentStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS),
101-
directoryName);
106+
File documentStorageDir = null;
107+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
108+
documentStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS),
109+
directoryName);
110+
}
102111

103112
// Create the storage directory if it does not exist
104113
if (!documentStorageDir.exists()) {
@@ -225,4 +234,76 @@ public static String getFileName(String p) {
225234
}
226235
return null;
227236
}
237+
238+
public static boolean copyFileFromAssetManagerToApplicationDirectory(Context context, String argAssetDir, String argDestinationDir) {
239+
boolean ret = true;
240+
try {
241+
AssetManager assetManager = context.getAssets();
242+
243+
String sdPath = context.getApplicationInfo().dataDir;
244+
String destDirPath = sdPath + addLeadingSlash(argDestinationDir);
245+
File destDir = new File(destDirPath);
246+
247+
createDir(destDir);
248+
249+
String[] files = assetManager.list(argAssetDir);
250+
251+
for (int i = 0; i < files.length; i++) {
252+
String absAssetFilePath = addTrailingSlash(argAssetDir) + files[i];
253+
String subFiles[] = assetManager.list(absAssetFilePath);
254+
255+
if (subFiles.length == 0) {
256+
// It is a file
257+
String destFilePath = addTrailingSlash(destDirPath) + files[i];
258+
copyAssetFile(context, absAssetFilePath, destFilePath);
259+
} else {
260+
// It is a sub directory
261+
copyFileFromAssetManagerToApplicationDirectory(context, absAssetFilePath, addTrailingSlash(argDestinationDir) + files[i]);
262+
}
263+
}
264+
}catch (Exception e){
265+
e.printStackTrace();
266+
ret = false;
267+
}
268+
return ret;
269+
}
270+
271+
public static void copyAssetFile(Context context, String assetFilePath, String destinationFilePath) throws IOException {
272+
InputStream in = context.getAssets().open(assetFilePath);
273+
OutputStream out = new FileOutputStream(destinationFilePath);
274+
275+
byte[] buf = new byte[1024];
276+
int len;
277+
while ((len = in.read(buf)) > 0)
278+
out.write(buf, 0, len);
279+
in.close();
280+
out.close();
281+
}
282+
283+
public static String addTrailingSlash(String path) {
284+
if (path.charAt(path.length() - 1) != '/') {
285+
path += "/";
286+
}
287+
return path;
288+
}
289+
290+
public static String addLeadingSlash(String path) {
291+
if (path.charAt(0) != '/') {
292+
path = "/" + path;
293+
}
294+
return path;
295+
}
296+
297+
public static void createDir(File dir) throws IOException {
298+
if (dir.exists()) {
299+
if (!dir.isDirectory()){
300+
throw new IOException("Can't create directory, a file is in the way");
301+
}
302+
} else {
303+
dir.mkdirs();
304+
if (!dir.isDirectory()){
305+
throw new IOException("Unable to create directory");
306+
}
307+
}
308+
}
228309
}

0 commit comments

Comments
 (0)