Copy file from the internal to the external storage in Android

不打扰是莪最后的温柔 提交于 2019-12-19 02:54:56

问题


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:

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

Approach #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();
    }
}

Approach #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" />

in AndroidManifest.xml and it does persist.

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

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.

In the approach #3, I decided to verify if the destination object exists and it creates a folder if needed, but when I check if I can write, the check returns false.

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

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.

Thanks.


回答1:


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

File dst = new File(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;
}

Thanks for the tips.




回答2:


Kotlin 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)


来源:https://stackoverflow.com/questions/31094071/copy-file-from-the-internal-to-the-external-storage-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!