AssetsUtils.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package com.xugame.gameconsole.util;
  2. import android.content.Context;
  3. import android.os.Environment;
  4. import android.os.Handler;
  5. import android.os.Looper;
  6. import android.os.Message;
  7. import java.io.File;
  8. import java.io.FileOutputStream;
  9. import java.io.InputStream;
  10. public class AssetsUtils {
  11. private static AssetsUtils instance;
  12. private static final int SUCCESS = 1;
  13. private static final int FAILED = 0;
  14. private Context context;
  15. private FileOperateCallback callback;
  16. private volatile boolean isSuccess=false;
  17. private String errorStr;
  18. public static AssetsUtils getInstance(Context context) {
  19. if (instance == null)
  20. instance = new AssetsUtils(context);
  21. return instance;
  22. }
  23. private AssetsUtils(Context context) {
  24. this.context = context;
  25. }
  26. private Handler handler = new Handler(Looper.getMainLooper()) {
  27. @Override
  28. public void handleMessage(Message msg) {
  29. super.handleMessage(msg);
  30. if (callback != null) {
  31. if (msg.what == SUCCESS) {
  32. callback.onSuccess();
  33. }
  34. if (msg.what == FAILED) {
  35. callback.onFailed(msg.obj.toString());
  36. }
  37. }
  38. }
  39. };
  40. public AssetsUtils copyAssetsToSD(final String srcPath, final String sdPath) {
  41. new Thread(new Runnable() {
  42. @Override
  43. public void run() {
  44. copyAssetsToDst(context, srcPath, sdPath);
  45. if (isSuccess)
  46. handler.obtainMessage(SUCCESS).sendToTarget();
  47. else
  48. handler.obtainMessage(FAILED, errorStr).sendToTarget();
  49. }
  50. }).start();
  51. return this;
  52. }
  53. public void setFileOperateCallback(FileOperateCallback callback) {
  54. this.callback = callback;
  55. }
  56. // private void copyAssetsAll(Context context,String dstPath){
  57. // String fileNames[] =context.getAssets().getLocales();
  58. // for (String temp:fileNames){
  59. // DebugUtil.i(TAG,""+temp);
  60. // }
  61. // }
  62. private void copyAssetsToDst(Context context, String srcPath, String dstPath) {
  63. try {
  64. String fileNames[] = context.getAssets().list(srcPath);
  65. if (fileNames.length > 0) {
  66. File file = new File(Environment.getExternalStorageDirectory(), dstPath);
  67. if (!file.exists()) file.mkdirs();
  68. for (String fileName : fileNames) {
  69. if (!srcPath.equals("")) { // assets 文件夹下的目录
  70. copyAssetsToDst(context, srcPath + File.separator + fileName, dstPath + File.separator + fileName);
  71. } else { // assets 文件夹
  72. copyAssetsToDst(context, fileName, dstPath + File.separator + fileName);
  73. }
  74. }
  75. } else {
  76. File outFile = new File(Environment.getExternalStorageDirectory(), dstPath);
  77. InputStream is = context.getAssets().open(srcPath);
  78. FileOutputStream fos = new FileOutputStream(outFile);
  79. byte[] buffer = new byte[1024];
  80. int byteCount;
  81. while ((byteCount = is.read(buffer)) != -1) {
  82. fos.write(buffer, 0, byteCount);
  83. }
  84. fos.flush();
  85. is.close();
  86. fos.close();
  87. }
  88. isSuccess = true;
  89. } catch (Exception e) {
  90. e.printStackTrace();
  91. errorStr = e.getMessage();
  92. isSuccess = false;
  93. }
  94. }
  95. public interface FileOperateCallback {
  96. void onSuccess();
  97. void onFailed(String error);
  98. }
  99. }