1 package org.xeli.dataapk; |
|
2 |
|
3 |
|
4 import java.io.File; |
|
5 import java.io.FileOutputStream; |
|
6 import java.io.IOException; |
|
7 import java.io.InputStream; |
|
8 import java.io.OutputStream; |
|
9 |
|
10 import android.content.Context; |
|
11 import android.content.res.AssetManager; |
|
12 import android.util.Log; |
|
13 |
|
14 public class AssetsToSDCard implements Runnable { |
|
15 |
|
16 public int INTERNAL_SDCARD = 0; |
|
17 public int EXTERNAL_SDCARD = 1; |
|
18 |
|
19 private Context context; |
|
20 private File outputDir; |
|
21 public AssetsToSDCard(Context c, boolean verifiedFreeSpace, String _outputDir){ |
|
22 context = c; |
|
23 outputDir = c.getExternalFilesDir("Data").getParentFile(); |
|
24 } |
|
25 |
|
26 private void copyFile(InputStream in, OutputStream out) throws IOException { |
|
27 byte[] buffer = new byte[1024]; |
|
28 int read; |
|
29 while((read = in.read(buffer)) != -1){ |
|
30 out.write(buffer, 0, read); |
|
31 } |
|
32 } |
|
33 |
|
34 private void visitAllFiles(AssetManager assManager, String[] childs, String file){ |
|
35 try { |
|
36 InputStream in; |
|
37 OutputStream out; |
|
38 if(childs.length == 0){ //file = a non directory file |
|
39 in = assManager.open(file); |
|
40 File f = new File(outputDir, file); |
|
41 out = new FileOutputStream(f); |
|
42 copyFile(in, out); |
|
43 }else{ //file = a directory |
|
44 for(String s : childs){ |
|
45 File f = new File(outputDir, file); |
|
46 f.mkdir(); |
|
47 String tmp = file + '/' + s; |
|
48 visitAllFiles(assManager, assManager.list(tmp), tmp); |
|
49 } |
|
50 } |
|
51 |
|
52 } catch (IOException e) { |
|
53 //TODO handle correctly |
|
54 Log.e("fail", file); |
|
55 e.printStackTrace(); |
|
56 } |
|
57 } |
|
58 |
|
59 public void run() {//Runs in it's own thread |
|
60 AssetManager assManager = context.getAssets(); |
|
61 |
|
62 try { |
|
63 Log.e("DataDownloader", "Starting to copy files"); |
|
64 visitAllFiles(assManager, assManager.list("Data"), "Data"); |
|
65 Log.e("DataDownloader", "Done copying files"); |
|
66 } catch (IOException e) { |
|
67 e.printStackTrace(); |
|
68 } |
|
69 } |
|
70 |
|
71 } |
|