Android的:如何复制文件从“资产”文件夹到SD卡? [英] Android: How to copy files from 'assets' folder to sdcard?
本文介绍了Android的:如何复制文件从“资产”文件夹到SD卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在资产
文件夹中的一些文件。我需要所有的人都复制到文件夹说/ SD卡/文件夹。我想从一个线程中执行此操作。我该怎么办呢?
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
这篇关于Android的:如何复制文件从“资产”文件夹到SD卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文