Assets下文件复制到sdcard
private void copyFile() {
AssetManager assetManager = this.getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open("xxxx.mp4");
String newFileName = Environment.getExternalStorageDirectory()+"/xxxx.mp4";
out = new FileOutputStream(newFileName);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
}
Sdcard文件夹复制
public static int copyFile(String fromFilePath, String toFilePath) {
File fromFile = new File(fromFilePath);
if (!fromFile.exists()) {
return -1;
}
File toFile = new File(toFilePath);
if (!toFile.exists()) {
toFile.mkdirs();
}
File[] fromFiles = fromFile.listFiles();
for (int i = 0; i < fromFiles.length; i++) {
if (fromFiles[i].isDirectory()) {
copyFile(fromFiles[i].getPath(), toFilePath + "/" + fromFiles[i].getName());
} else {
copyDirFile(fromFiles[i].getPath(), toFilePath + "/" + fromFiles[i].getName());
}
}
return 0;
}
public static int copyDirFile(String fromFilePath, String toFilePath) {
if (TextUtils.isEmpty(fromFilePath) || TextUtils.isEmpty(toFilePath)) {
return -1;
}
if (new File(fromFilePath).exists() == false) {
return -1;
}
if (new File(toFilePath).exists()) {
return 0;
}
try {
InputStream inStream = new FileInputStream(fromFilePath);
OutputStream outStream = new FileOutputStream(toFilePath);
byte[] bytes = new byte[1024];
int i = 0;
while ((i = inStream.read(bytes)) > 0) {
outStream.write(bytes, 0, i);
}
inStream.close();
outStream.close();
return 0;
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}