3 import android
.content
.Context
;
4 import android
.content
.res
.AssetManager
;
5 import android
.os
.Environment
;
8 import java
.io
.FileOutputStream
;
9 import java
.io
.IOException
;
10 import java
.io
.InputStream
;
11 import java
.io
.OutputStream
;
13 import android
.util
.Log
;
15 public class CopyAssets
{
17 private static boolean ExtStorageMounted() {
18 return Environment
.MEDIA_MOUNTED
.equals(Environment
.getExternalStorageState());
21 private static boolean ExtStorageReadonly() {
22 return Environment
.MEDIA_MOUNTED_READ_ONLY
.equals(Environment
.getExternalStorageState());
25 private static void CopyFile(InputStream in
, OutputStream out
)
28 byte[] buffer
= new byte[1024];
30 while ((read
= in
.read(buffer
)) != -1) {
31 out
.write(buffer
, 0, read
);
35 public static void copyAssets(Context context
, String prefix
) {
36 AssetManager assetManager
= context
.getAssets();
37 String
[] files
= null;
39 files
= assetManager
.list(prefix
);
40 } catch (IOException e
) {
41 Log
.e("tag", "Failed to get asset file list.", e
);
44 for (String filename
: files
) {
45 InputStream in
= null;
46 OutputStream out
= null;
48 if (ExtStorageMounted() && !ExtStorageReadonly()) {
49 /* Get External Storage Path */
50 File f
= new File(context
.getExternalFilesDir(null).getAbsolutePath(), prefix
);
54 File outFile
= new File(context
.getExternalFilesDir(null).getAbsolutePath(), prefix
+ "/" + filename
);
55 if (!outFile
.exists()) {
56 in
= assetManager
.open(prefix
+ "/" + filename
);
57 out
= new FileOutputStream(outFile
);
61 /* Get Internal Storage Path */
62 File f
= new File(context
.getFilesDir().getAbsolutePath(), prefix
);
66 File outFile
= new File(context
.getFilesDir().getAbsolutePath(), prefix
+ "/" + filename
);
67 if (!outFile
.exists()) {
68 in
= assetManager
.open(prefix
+ "/" + filename
);
69 out
= new FileOutputStream(outFile
);
73 } catch(IOException e
) {
74 Log
.e("tag", "Failed to copy asset file: " + filename
, e
);
80 } catch (IOException e
) {}
87 } catch (IOException e
) {}