5

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?

Community
  • 1
  • 1
user1177292
  • 273
  • 6
  • 18

3 Answers3

2

Why cant you use rename?

File sd=Environment.getExternalStorageDirectory();
// File (or directory) to be moved
String sourcePath="/.Images/"+imageTitle;
File file = new File(sd,sourcePath);
// Destination directory
boolean success = file.renameTo(new File(sd, imageTitle));
Jithu
  • 1,478
  • 1
  • 13
  • 21
2

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)
        {}
    }
}
lulumeya
  • 1,619
  • 11
  • 14
1

I know that this question has been answered ages ago, but I found copying the entire file is a harsh method... Here's what I do if someone needs it :

static public boolean moveFile(String oldfilename, String newFolderPath, String newFilename) {
    File folder = new File(newFolderPath);
    if (!folder.exists())
        folder.mkdirs();

    File oldfile = new File(oldfilename);
    File newFile = new File(newFolderPath, newFilename);

    if (!newFile.exists())
        try {
            newFile.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    return oldfile.renameTo(newFile);
}
TheSquad
  • 7,385
  • 8
  • 40
  • 79
  • This works if the old and new files are on same mount point. From [docs](http://developer.android.com/reference/java/io/File.html#renameTo%28java.io.File%29) "Both paths be on the same mount point." – Diederik Aug 01 '14 at 09:28
  • Are you sure that you need to create a new file if one does not exist? – Ilya Gazman Apr 09 '19 at 17:26
  • @IlyaGazman yes, no other way to create a new file, if you want a move, since renameTo take a File object – TheSquad Apr 10 '19 at 09:22