将文件从内部存储复制到 Android 中的外部存储

Copy file from the internal to the external storage in Android

我的应用程序(Android API 15) 拍了一张图片并存储在内存的文件夹中。现在,我想将此文件复制到外部存储中的另一个文件夹,例如/sdcard/myapp。我尝试了以下方法:

方法 #1:

private void copyFile(File src, File dst) throws IOException {

    File from = new File(src.getPath());
    File to = new File(dst.getPath());
    from.renameTo(to);
}

方法 #2:

private void copyFile(File src, File dst) throws IOException {

    FileChannel inChannel = null;
    FileChannel outChannel = null;

    try {
        inChannel = new FileInputStream(src).getChannel();
        outChannel = new FileOutputStream(dst).getChannel();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    try {
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
    }
}

方法 #3:

private void copyFile(File src, File dst) throws IOException {

    FileInputStream inStream = new FileInputStream(src);

    if (!dst.exists()) {
        dst.mkdir();
    }

    if (!dst.canWrite()) {
        System.out.print("CAN'T WRITE");
        return;
    }

    FileOutputStream outStream = new FileOutputStream(dst);
    FileChannel inChannel = inStream.getChannel();
    FileChannel outChannel = outStream.getChannel();
    inChannel.transferTo(0, inChannel.size(), outChannel);
    inStream.close();
    outStream.close();
}

None 这些方法无法解决我的任务。在检查了一些相关主题,我发现的唯一建议是验证

的持久性
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

AndroidManifest.xml 中,它确实存在。

方法 #1 完成执行,但没有文件夹和文件被复制。

方法 #2 中,应用程序失败,异常 java.lang.NullPointerExceptionoutChannel = new FileOutputStream(dst).getChannel();,但是对象 dst 不是 null。

方法 #3 中,我决定验证目标对象是否存在,如果需要它会创建一个文件夹,但是当我检查我是否会写,检查 returns false.

我尝试了一些额外的方法,成功创建了一个空文件夹,但没有真正复制文件。

由于这是我迈向 Android 的第一步,我觉得我错过了一些小东西。请指点我,如何将文件从一个文件夹复制到 Android 中的另一个文件夹,包括从内部存储器移动到外部存储器的文件。

我解决了我的问题。问题出在目标路径中,在原始代码中:

File dst = new File(dstPath);

变量dstPath有完整的目标路径,包括文件名,这是错误的。这是正确的代码片段:

String dstPath = Environment.getExternalStorageDirectory() + File.separator + "myApp" + File.separator;
File dst = new File(dstPath);

exportFile(pictureFile, dst);

private File exportFile(File src, File dst) throws IOException {

    //if folder does not exist
    if (!dst.exists()) {
        if (!dst.mkdir()) {
            return null;
        }
    }

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File expFile = new File(dst.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    FileChannel inChannel = null;
    FileChannel outChannel = null;

    try {
        inChannel = new FileInputStream(src).getChannel();
        outChannel = new FileOutputStream(expFile).getChannel();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    try {
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
    }

    return expFile;
}

感谢您的提示。

科特林 https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/java.io.-file/copy-to.html

    // video is some file in internal storage
    val to = File(Environment.getExternalStorageDirectory().absolutePath + "/destination.file")
    video.copyTo(to, true)