如何移动/重命名内部应用程序的存储文件在Android上的外部存储?应用程序、重命名、文件、Android

2023-09-12 01:51:23 作者:我是你的蓝胖子

我从网上下载文件和流数据保存到由getFilesDir().

I am downloading files from the internet and saving the streaming data to a temp file in my app's internal storage given by getFilesDir().

在下载完成后,我需要将临时文件移动到外部存储器(通常是SD卡),我的下载目录。出于某种原因,不过,File.renameTo()不工作这一点。我猜有一个问题,因为这是两个独立的文件系统,但我仍然可以直接下载到SD卡和文件URI是正确的。

Once the download is complete, I need to move the temp file to my download directory on External Memory (usually an SD Card). For some reason though, File.renameTo() isn't working for this. I'm guessing there's a problem because it's two separate file systems, but I can still download directly to the SD Card and the file URIs are correct.

有没有从内部存储器传输该文件到外部或做我必须做一个字节流复制和删除原来的另一种简单,快捷的方式?

Is there another simple and quick way to transfer that file from internal memory to external or do I have to do a byte stream copy and delete the original?

推荐答案

从内部存储器中的文件通过下面这段code复制到SD卡,反之亦然:

To copy files from internal memory to SD card and vice-versa using following piece of code:

public static void copyFile(File src, File dst) throws IOException
{
    FileChannel inChannel = new FileInputStream(src).getChannel();
    FileChannel outChannel = new FileOutputStream(dst).getChannel();
    try
    {
        inChannel.transferTo(0, inChannel.size(), outChannel);
    }
    finally
    {
        if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
    }
}

和 - 它的工作原理...

And - it works...