|
1 | 1 | package com.bvtech.toolslibrary.utility;
|
2 | 2 |
|
| 3 | +import android.content.Context; |
| 4 | +import android.content.res.AssetManager; |
3 | 5 | import android.os.Environment;
|
4 | 6 | import android.util.Log;
|
5 | 7 |
|
6 | 8 | import java.io.File;
|
| 9 | +import java.io.FileOutputStream; |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.io.OutputStream; |
7 | 13 | import java.text.SimpleDateFormat;
|
8 | 14 | import java.util.Date;
|
9 | 15 | import java.util.Locale;
|
@@ -97,8 +103,11 @@ public static File createOutputVideoFile(String directoryName) {
|
97 | 103 |
|
98 | 104 | public static File createOutputDocumentFile(String directoryName) {
|
99 | 105 | // 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 | + } |
102 | 111 |
|
103 | 112 | // Create the storage directory if it does not exist
|
104 | 113 | if (!documentStorageDir.exists()) {
|
@@ -225,4 +234,76 @@ public static String getFileName(String p) {
|
225 | 234 | }
|
226 | 235 | return null;
|
227 | 236 | }
|
| 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 | + } |
228 | 309 | }
|
0 commit comments