3

I have been trying to move files within the sd card but to no avail: Here is the code:

try {
            File sd=Environment.getExternalStorageDirectory();
            // File (or directory) to be moved
            String sourcePath="mnt/sdcard/.Images/"+imageTitle;
            File file = new File(sd,sourcePath);
            // Destination directory
            String destinationPath="mnt/sdcard/"+imageTitle;
            File dir = new File(sd,destinationPath);

            // Move file to new directory
            boolean success = file.renameTo(new File(dir, file.getName()));
            if (!success) {
                handler.post(new Runnable(){
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), "File moved", Toast.LENGTH_LONG).show();
                    }
                });
            }

        } 
        catch (Exception e) {
        }

I dont know wasup.Will appreciate the help.

John
  • 243
  • 2
  • 6
  • 15

1 Answers1

12

First: If you've gotten the external directory, there is no need to add it to the beginning of your sourcepath and destinationpath

Second, the destinationPath seems unnecessary as it looks like you just want to move it to the sdcard's root folder.

It should be

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));
Otra
  • 8,108
  • 3
  • 34
  • 49
  • 1
    note that the destination folder needs to exists. File.mkdirs can come in handy for this – ılǝ Aug 29 '14 at 08:47