I need to move files from one location on the users sdcard to another location on the sdcard
Currently I am doing this with File.renameTo
e.g. from sdcard/test/one.txt to sdcard/test2/two.txt
Some users have reported the file move feature is not working.
I came across the link below:
How to copy files from 'assets' folder to sdcard?
So what is the best way to move a file from one directory to another on the sdcard?
try copy with these codes and check files and remove original one.
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)
{}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With