package com.basic.security.utils;
|
|
import android.content.Context;
|
import android.os.Environment;
|
import android.os.Handler;
|
import android.os.Looper;
|
import android.os.Message;
|
|
import com.basic.security.base.BaseApplication;
|
|
import java.io.File;
|
import java.io.FileOutputStream;
|
import java.io.InputStream;
|
|
|
public class CopyToSdcard {
|
private static final int SUCCESS = 1;
|
private static final int FAILED = 0;
|
private static CopyToSdcard instance;
|
private Context context;
|
private FileOperateCallback callback;
|
private volatile boolean isSuccess;
|
private String errorStr;
|
private Handler handler = new Handler(Looper.getMainLooper()) {
|
@Override
|
public void handleMessage(Message msg) {
|
super.handleMessage(msg);
|
if (callback != null) {
|
if (msg.what == SUCCESS) {
|
callback.onSuccess();
|
}
|
if (msg.what == FAILED) {
|
callback.onFailed(msg.obj.toString());
|
}
|
}
|
}
|
};
|
|
private CopyToSdcard(Context context) {
|
this.context = context;
|
}
|
|
public static CopyToSdcard getInstance(Context context) {
|
if (instance == null)
|
instance = new CopyToSdcard(context);
|
return instance;
|
}
|
|
public CopyToSdcard copyAssetsToSD(final String srcPath, final String sdPath) {
|
BaseApplication.getApplication().executorService.execute(() -> {
|
copyAssetsToDst(context, srcPath, sdPath);
|
if (isSuccess)
|
handler.obtainMessage(SUCCESS).sendToTarget();
|
else
|
handler.obtainMessage(FAILED, errorStr).sendToTarget();
|
});
|
return this;
|
}
|
|
public void setFileOperateCallback(FileOperateCallback callback) {
|
this.callback = callback;
|
}
|
|
private void copyAssetsToDst(Context context, String srcPath, String dstPath) {
|
try {
|
String fileNames[] = context.getAssets().list(srcPath);
|
if (fileNames.length > 0) {
|
File file = new File(Environment.getExternalStorageDirectory(), dstPath);
|
if (!file.exists()) file.mkdirs();
|
for (String fileName : fileNames) {
|
if (!srcPath.equals("")) { // assets 文件夹下的目录
|
copyAssetsToDst(context, srcPath + File.separator + fileName, dstPath + File.separator + fileName);
|
} else { // assets 文件夹
|
copyAssetsToDst(context, fileName, dstPath + File.separator + fileName);
|
}
|
}
|
} else {
|
File outFile = new File(Environment.getExternalStorageDirectory(), dstPath);
|
InputStream is = context.getAssets().open(srcPath);
|
FileOutputStream fos = new FileOutputStream(outFile);
|
byte[] buffer = new byte[1024];
|
int byteCount;
|
while ((byteCount = is.read(buffer)) != -1) {
|
fos.write(buffer, 0, byteCount);
|
}
|
fos.flush();
|
is.close();
|
fos.close();
|
}
|
isSuccess = true;
|
} catch (Exception e) {
|
e.printStackTrace();
|
errorStr = e.getMessage();
|
isSuccess = false;
|
}
|
}
|
|
public interface FileOperateCallback {
|
void onSuccess();
|
|
void onFailed(String error);
|
}
|
|
}
|