Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving a file on android

Tags:

android

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?

like image 689
user1177292 Avatar asked Feb 03 '12 00:02

user1177292


1 Answers

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)
        {}
    }
}
like image 87
lulumeya Avatar answered Sep 28 '22 17:09

lulumeya