0

I want to copy all the contents of a folder to another folder on SDCard. I want to do it at OS level. I've tried using the following command : cp -a /source/. /dest/, this doesn't work, It says Permission Denied as my device is not rooted. However an interesting thing is that it allows me to execute rm - r source

String deleteCmd = "rm -r " + sourcePath;
            Runtime delete_runtime = Runtime.getRuntime();
            try {
                delete_runtime.exec(deleteCmd);
            } catch (IOException e) {
                Log.e("TAG", Log.getStackTraceString(e));
            }

Kindly tell me if there exists a way through which i can achieve this at OS level else my last resort will be this LINK. Thanks in advance.

CodeWarrior
  • 5,026
  • 6
  • 30
  • 46

3 Answers3

1

After researching more i found the perfect solution that suits my requirement. The file copy is TREMENDOUSLY FAST.

The mv command doest the magic for me, it moves all the files inside the source folder to the destination folder and after copying it deletes the source folder.

String copyCmd = "mv " + sourcePath + " " + destinationPath;
Runtime copy_runtime = Runtime.getRuntime();
try {
        copy_runtime.exec(copyCmd);
     } catch (IOException e) {
        Log.d("TAG", Log.getStackTraceString(e));
     }
CodeWarrior
  • 5,026
  • 6
  • 30
  • 46
  • mv is different then copy, has its advantages and drawbacks. – skoperst Jan 09 '14 at 11:28
  • @skoperst Yes i know that my friend but as i said "it suits my requirement" so its fine for me. However can u kindly enlighten on its drawbacks as im unaware of them. – CodeWarrior Jan 09 '14 at 18:31
0

Your error is "permission denied", either you don't have the permission to execute "cp" binary or you don't have permission to create directories in sdcard or alot of other stuff that can go wrong.

Use adb shell to learn more about cp command, its located in /system/bin/.

Or

You can download terminal emulator application and try to run the command from shell.

Use ls-l /system/bin to check for permissions.

Beside of all this, don't forget that your sdcard has FAT filesystem while cp -a uses combination of chmod and utime which can also be out of your permission scope. And I'm not talking about doing chmod on a FAT fs is not a good idea to start with. Unless you fully understand the issues you are facing here, I would also advise using the LINK you provided.

skoperst
  • 2,259
  • 1
  • 23
  • 35
  • Thanks for answering mate, but i researched it a bit more and found that since the linux kernel that android uses is a stripped down version and cp command isn't included in it and yes i've tried it on adb shell first and then only i put this question up here. – CodeWarrior Jan 09 '14 at 07:29
-1
public void copyDirectory(File sourceLocation , File targetLocation)
throws IOException {

    if (sourceLocation.isDirectory()) {
        if (!targetLocation.exists() && !targetLocation.mkdirs()) {
            throw new IOException("Cannot create dir " + targetLocation.getAbsolutePath());
        }

        String[] children = sourceLocation.list();
        for (int i=0; i<children.length; i++) {
            copyDirectory(new File(sourceLocation, children[i]),
                    new File(targetLocation, children[i]));
        }
    } else {

        // make sure the directory we plan to store the recording in exists
        File directory = targetLocation.getParentFile();
        if (directory != null && !directory.exists() && !directory.mkdirs()) {
            throw new IOException("Cannot create dir " + directory.getAbsolutePath());
        }

        InputStream in = new FileInputStream(sourceLocation);
        OutputStream out = new FileOutputStream(targetLocation);

        // Copy the bits from instream to outstream
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }
}
Mourice
  • 597
  • 1
  • 5
  • 17
  • This doesn't help mate. See my LINK that i've provided in the end of my question, it has the similar implementation which im trying to find a workaround. – CodeWarrior Jan 06 '14 at 11:09