3 import android
.content
.Context
;
4 import android
.content
.res
.AssetManager
;
7 import java
.io
.FileOutputStream
;
8 import java
.io
.IOException
;
9 import java
.io
.InputStream
;
10 import java
.io
.OutputStream
;
12 import android
.util
.Log
;
14 public class CopyAssets
{
16 public static void copyAssets(Context context
, String prefix
) {
17 AssetManager assetManager
= context
.getAssets();
18 String
[] files
= null;
20 files
= assetManager
.list(prefix
);
21 } catch (IOException e
) {
22 Log
.e("tag", "Failed to get asset file list.", e
);
24 if (files
!= null) for (String filename
: files
) {
25 InputStream in
= null;
26 OutputStream out
= null;
28 File f
= new File(context
.getExternalFilesDir(null), prefix
);
32 File outFile
= new File(context
.getExternalFilesDir(null), prefix
+ "/" + filename
);
33 if (!outFile
.exists()) {
34 in
= assetManager
.open(prefix
+ "/" + filename
);
35 out
= new FileOutputStream(outFile
);
38 } catch(IOException e
) {
39 Log
.e("tag", "Failed to copy asset file: " + filename
, e
);
46 } catch (IOException e
) {
55 } catch (IOException e
) {
63 public static void copyFile(InputStream in
, OutputStream out
) throws IOException
{
64 byte[] buffer
= new byte[1024];
66 while((read
= in
.read(buffer
)) != -1){
67 out
.write(buffer
, 0, read
);