我想将XML文件从res / raw文件夹复制到SD卡。这个问题实际上是针对ODK收集的。但任何帮助将不胜感激。我在网上查看过Android: How to create a directory on the SD Card and copy files from /res/raw to it?和其他类似的帖子,但仍然无法复制。也许是因为我正在开发ODK Collect。
这是我复制文件的代码:
try {
InputStream in = getResources().openRawResource(R.raw.problem2);
OutputStream out = new FileOutputStream(Collect.FORMS_PATH+"/problem2");
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
catch(IOException e) { }
提前致谢。
最佳答案
尝试这个:
private void copyFiles() throws IOException{
InputStream myInput = m_Context.getAssets().open(FILE_NAME_KEPT_IN_ASSET_FOLDER);
String outFileName = "/data/data/your.package.name/folder/";
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}