如何将一个xml文件从res / raw文件夹复制到android的sd卡?

时间:2011-07-19 10:47:30

标签: android

我想将res / raw文件夹中的xml文件复制到SD卡。这个问题实际上特定于ODK Collect。但任何帮助将不胜感激。我查看过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) { }

提前致谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

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();

    }