如何将文件从“资产"文件夹复制到 SD 卡? [英] How to copy files from 'assets' folder to sdcard?
本文介绍了如何将文件从“资产"文件夹复制到 SD 卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 assets
文件夹中有几个文件.我需要将它们全部复制到一个文件夹中,比如/sdcard/folder.我想从线程中执行此操作.我该怎么做?
I have a few files in the assets
folder. I need to copy all of them to a folder say /sdcard/folder. I want to do this from within a thread. How do I do it?
推荐答案
如果其他人遇到同样的问题,我就是这样做的
If anyone else is having the same problem, this is how I did it
private void copyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
if (files != null) for (String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
File outFile = new File(getExternalFilesDir(null), filename);
out = new FileOutputStream(outFile);
copyFile(in, out);
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// NOOP
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
// NOOP
}
}
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
参考:使用 Java 移动文件
这篇关于如何将文件从“资产"文件夹复制到 SD 卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文