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

Copy file from the internal to the external storage in Android(在 Android 中将文件从内部存储复制到外部存储)

本文介绍了在 Android 中将文件从内部存储复制到外部存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My app (Android API 15) makes a picture and stores it in the internal memory's folder. Now, I want to copy this file to another folder inside of the external storage, e.g. /sdcard/myapp. I tried the following approaches:

方法一:

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 of these methods doesn't solve my task. In checked a number of related topics, and the only suggestion I found is to verify the persistence of

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

AndroidManifest.xml 中,它确实存在.

in AndroidManifest.xml and it does persist.

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

The approach #1 finishes the execution, but no folder and files are copied.

方法 #2 中,应用程序失败并出现异常 java.lang.NullPointerException at outChannel = new FileOutputStream(dst).getChannel();,但对象 dst 不为空.

In the approach #2, the app fails with the exception java.lang.NullPointerException at outChannel = new FileOutputStream(dst).getChannel();, but the object dst is not a null.

方法#3中,我决定验证目标对象是否存在并在需要时创建一个文件夹,但是当我检查是否可以编写时,检查返回 false.

我尝试了几种其他方法,成功创建了一个空文件夹,但没有真正复制任何文件.

I tried a couple of additional approaches, which succeeded to create an empty folder, but no files are really copied.

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

Since this is my very first step towards Android, I feel I miss some small thing. Please, point me, how to copy a file from one folder to another folder in Android, including file moving from internal to external memory.

推荐答案

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

I solved my issue. The problem was in the destination path, in the original code:

File dst = new File(dstPath);

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

the variable dstPath had the full destination path, including the name of the file, which is wrong. Here is the correct code fragment:

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

感谢您的提示.

这篇关于在 Android 中将文件从内部存储复制到外部存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 Android 中将文件从内部存储复制到外部存储

基础教程推荐