美好的一天!
我刚刚开始为 android 开发。在我的应用中,我需要将 Assets 文件夹中的项目复制到内部存储中。
我在 SO 上进行了很多搜索,包括将其复制到外部存储的这个。 How to copy files from 'assets' folder to sdcard?
这就是我想要实现的目标: 我有一个目录已经存在于内部存储中,为 X>Y>Z。我需要一个文件复制到 Y,另一个复制到 Z。
谁能帮我提供一个代码 fragment ?我真的不知道该怎么做。
对不起,我的英语不好。
非常感谢。
最佳答案
使用
String out= Environment.getExternalStorageDirectory().getAbsolutePath() + "/X/Y/Z/" ;
File outFile = new File(out, Filename);
在您的引用文献中编辑后。链接答案。
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);
}
for(String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
String outDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/X/Y/Z/" ;
File outFile = new File(outDir, filename);
out = new FileOutputStream(outFile);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
}
}
}
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 - 将 Assets 复制到内部存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19218775/