如何移动/重命名内部应用程序的存储文件在Android上的外部存储? [英] How to move/rename file from internal app storage to external storage on Android?

查看:112
本文介绍了如何移动/重命名内部应用程序的存储文件在Android上的外部存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从网上下载文件和流数据保存到由<给定我的应用程序的内部存储临时文件href="http://developer.android.com/reference/android/content/Context.html#getFilesDir%28%29">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...

这篇关于如何移动/重命名内部应用程序的存储文件在Android上的外部存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆