将文件复制到SD卡

时间:2014-02-03 12:16:19

标签: android copy android-sdcard

我有一个Android应用程序将文件复制到SD卡

我的代码块如下所示:

private void CopyXmlFile2SdCard() {
    AssetManager asstMan = getAssets();
    try {
        InputStream in = null;
        OutputStream out = null;
        in = asstMan.open("platform.xml");
        File outFile = new File(getExternalFilesDir(null), "platform.xml");
        out = new FileOutputStream(outFile);
        CopyFile(in, out);
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;

    } catch (IOException e) {
        Log.e("Copy Error", "Failed to get asset file");
    }

}

我的复制方法是:

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

我遇到错误“文件未找到”:

out = new FileOutputStream(outFile);

我也已经获得了复制到sdcard(write_external_storage)所需的权限。 任何建议将不胜感激..

1 个答案:

答案 0 :(得分:1)

API LEVEL 8中提供了

getExternalFilesDir。使用Environment.getExternalStorageDirectory()进行更改。

编辑。

另外,请务必在设备/模拟器中添加SD卡