Question

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.

Was it helpful?

Solution

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));
     }

OTHER TIPS

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.

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();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top