[Android] Add file copy methods

Geoffrey Métais git at videolan.org
Fri Feb 5 16:13:10 CET 2016


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Feb  5 15:48:36 2016 +0100| [f6776fb46ae76ec5bb49e4d88a6b7bc775c7af28] | committer: Geoffrey Métais

Add file copy methods

> https://code.videolan.org/videolan/vlc-android/commit/f6776fb46ae76ec5bb49e4d88a6b7bc775c7af28
---

 .../src/org/videolan/vlc/util/FileUtils.java       | 92 ++++++++++++++++++++++
 1 file changed, 92 insertions(+)

diff --git a/vlc-android/src/org/videolan/vlc/util/FileUtils.java b/vlc-android/src/org/videolan/vlc/util/FileUtils.java
index dc1b22c..e233d11 100644
--- a/vlc-android/src/org/videolan/vlc/util/FileUtils.java
+++ b/vlc-android/src/org/videolan/vlc/util/FileUtils.java
@@ -25,6 +25,7 @@ package org.videolan.vlc.util;
 
 import android.annotation.TargetApi;
 import android.content.ContentResolver;
+import android.content.res.AssetManager;
 import android.database.Cursor;
 import android.net.Uri;
 import android.os.Build;
@@ -34,7 +35,15 @@ import android.text.TextUtils;
 import org.videolan.libvlc.util.AndroidUtil;
 import org.videolan.vlc.VLCApplication;
 
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 
 public class FileUtils {
 
@@ -79,6 +88,89 @@ public class FileUtils {
         }
     }
 
+    public static boolean copyAssetFolder(AssetManager assetManager, String fromAssetPath, String toPath) {
+        try {
+            String[] files = assetManager.list(fromAssetPath);
+            if (files.length == 0)
+                return false;
+            new File(toPath).mkdirs();
+            boolean res = true;
+            for (String file : files)
+                if (file.contains("."))
+                    res &= copyAsset(assetManager,
+                            fromAssetPath + "/" + file,
+                            toPath + "/" + file);
+                else
+                    res &= copyAssetFolder(assetManager,
+                            fromAssetPath + "/" + file,
+                            toPath + "/" + file);
+            return res;
+        } catch (Exception e) {
+            e.printStackTrace();
+            return false;
+        }
+    }
+
+    private static boolean copyAsset(AssetManager assetManager,
+                                     String fromAssetPath, String toPath) {
+        InputStream in = null;
+        OutputStream out = null;
+        try {
+            in = assetManager.open(fromAssetPath);
+            new File(toPath).createNewFile();
+            out = new FileOutputStream(toPath);
+            copyFile(in, out);
+            out.flush();
+            return true;
+        } catch(Exception e) {
+            e.printStackTrace();
+            return false;
+        } finally {
+            Util.close(in);
+            Util.close(out);
+        }
+    }
+
+    public static void copyFile(InputStream in, OutputStream out) throws IOException {
+        byte[] buffer = new byte[1024];
+        int read;
+        while((read = in.read(buffer)) != -1){
+          out.write(buffer, 0, read);
+        }
+    }
+
+    public static boolean copyFile(File src, File dst){
+        boolean ret = true;
+        if (src.isDirectory()) {
+            File[] filesList = src.listFiles();
+            dst.mkdirs();
+            for (File file : filesList)
+                ret &= copyFile(file, new File(dst, file.getName()));
+        } else if (src.isFile()) {
+            InputStream in = null;
+            OutputStream out = null;
+            try {
+                in = new BufferedInputStream(new FileInputStream(src));
+                out = new BufferedOutputStream(new FileOutputStream(dst));
+
+                // Transfer bytes from in to out
+                byte[] buf = new byte[1024];
+                int len;
+                while ((len = in.read(buf)) > 0) {
+                    out.write(buf, 0, len);
+                }
+                return true;
+            } catch (FileNotFoundException e) {
+            } catch (IOException e) {
+            } finally {
+                Util.close(in);
+                Util.close(out);
+            }
+            return false;
+        }
+        return ret;
+    }
+
     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
     public static boolean deleteFile (String path){
         boolean deleted = false;



More information about the Android mailing list