zhangzengfei
2022-01-10 4496b59ab27d569df1da7ef634e02273b3a9618a
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
package com.basic.security.utils;
 
import android.content.res.AssetManager;
import android.util.Log;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 
public class AssetHelper {
 
    static String TARGET_BASE_PATH = "/sdcard/appname/voices/";
//    private 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 void copyAssets(AssetManager assetManager, String outDir) {
        TARGET_BASE_PATH = outDir + "/";
        copyFileOrDir(assetManager, ""); // copy all files in assets folder in my project
//        String[] files = null;
//        try {
//            files = assetManager.list("");
//        } catch (IOException e) {
//            Log.e("tag", "Failed to get asset file list.", e);
//        }
//        for(String filename : files) {
//            InputStream in = null;
//            OutputStream out = null;
//            try {
//                System.out.println("filename="+filename+",outDir="+outDir);
//                in = assetManager.open(filename);
//                //String outDir = c.getFilesDir().getAbsolutePath() + "/X/Y/Z/" ;
//
//                File outFile = new File(outDir, filename);
//
//                out = new FileOutputStream(outFile);
//                copyFile(in, out);
//                in.close();
//                in = null;
//                out.flush();
//                out.close();
//                out = null;
//            } catch(Exception e) {
//                //Log.e("tag", "Failed to copy asset file: " + filename, e);
//            }
//        }
    }
 
    private static void copyFileOrDir(AssetManager assetManager, String path) {
        String assets[] = null;
        try {
//            Log.i("tag", "copyFileOrDir() "+path);
            assets = assetManager.list(path);
            if (assets.length == 0) {
                copyFile(assetManager, path);
            } else {
                String fullPath = TARGET_BASE_PATH + path;
//                Log.i("tag", "path="+fullPath);
                File dir = new File(fullPath);
                if (!dir.exists() && !path.startsWith("images") && !path.startsWith("sounds") && !path.startsWith("webkit"))
                    if (!dir.mkdirs()) {
//                        Log.i("tag", "could not create dir " + fullPath);
                    }
                for (int i = 0; i < assets.length; ++i) {
                    String p;
                    if (path.equals(""))
                        p = "";
                    else
                        p = path + "/";
 
                    if (!path.startsWith("images") && !path.startsWith("sounds") && !path.startsWith("webkit"))
                        copyFileOrDir(assetManager, p + assets[i]);
                }
            }
        } catch (IOException ex) {
            Log.e("tag", "I/O Exception", ex);
        }
    }
 
    private static void copyFile(AssetManager assetManager, String filename) {
 
        InputStream in = null;
        OutputStream out = null;
        String newFileName = null;
        try {
//            Log.i("tag", "copyFile() "+filename);
            in = assetManager.open(filename);
            if (filename.endsWith(".jpg")) // extension was added to avoid compression on APK file
                newFileName = TARGET_BASE_PATH + filename.substring(0, filename.length() - 4);
            else
                newFileName = TARGET_BASE_PATH + filename;
            out = new FileOutputStream(newFileName);
 
            byte[] buffer = new byte[1024];
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        } catch (Exception e) {
            Log.e("tag", "Exception in copyFile() of " + newFileName);
            Log.e("tag", "Exception in copyFile() " + e.toString());
        }
 
    }
}