如何以编程方式将文件从内部内存移动到android中的SD卡?

时间:2015-06-26 17:54:11

标签: android file move

如何将文件从设备的内部存储器移动到android中的外部存储器?请提供代码示例。我的代码在

之下
    private void moveFile(File file, File dir) throws IOException {
    File newFile = new File(dir, file.getName());
    FileChannel outputChannel = null;
    FileChannel inputChannel = null;
    try {
        outputChannel = new FileOutputStream(newFile).getChannel();
        inputChannel = new FileInputStream(file).getChannel();
        inputChannel.transferTo(0, inputChannel.size(), outputChannel);
        inputChannel.close();
        file.delete();
    } finally {
        if (inputChannel != null) inputChannel.close();
        if (outputChannel != null) outputChannel.close();
    }

}

1 个答案:

答案 0 :(得分:1)

从路径中删除尾部斜杠。因为example.png不是目录,所以不需要它。实际上不要硬编码SD卡的路径,因为它可能因设备而异。尝试使用Environment.getExternalStorageDirectory()获取sdcard的路径,然后在最后添加任何尾随路径。

请查看documentation