gpt4 book ai didi

android - 在android上移动文件

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:52:34 24 4
gpt4 key购买 nike

我需要将文件从用户 sdcard 上的一个位置移动到 sdcard 上的另一个位置

目前我正在用 File.renameTo 做这个

例如从 sdcard/test/one.txt 到 sdcard/test2/two.txt

一些用户报告文件移动功能无法正常工作。

我看到了下面的链接:

How to copy files from 'assets' folder to sdcard?

那么将文件从 SD 卡上的一个目录移动到另一个目录的最佳方法是什么?

最佳答案

尝试使用这些代码进行复制并检查文件并删除原始文件。

private void backup(File sourceFile)
{
FileInputStream fis = null;
FileOutputStream fos = null;
FileChannel in = null;
FileChannel out = null;

try
{
File backupFile = new File(backupDirectory.getAbsolutePath() + seprator + sourceFile.getName());
backupFile.createNewFile();

fis = new FileInputStream(sourceFile);
fos = new FileOutputStream(backupFile);
in = fis.getChannel();
out = fos.getChannel();

long size = in.size();
in.transferTo(0, size, out);
}
catch (Throwable e)
{
e.printStackTrace();
}
finally
{
try
{
if (fis != null)
fis.close();
}
catch (Throwable ignore)
{}

try
{
if (fos != null)
fos.close();
}
catch (Throwable ignore)
{}

try
{
if (in != null && in.isOpen())
in.close();
}
catch (Throwable ignore)
{}

try
{
if (out != null && out.isOpen())
out.close();
}
catch (Throwable ignore)
{}
}
}

关于android - 在android上移动文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9121888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com