-1

I want to copy file from one location to another using root or su in android 13. example: copy from /storage/emulated/0/super/hi.txt copy to /storage/emulated/0/

i have tried this Runtime.getRuntime().exec("mv /storage/emulated/0/super/hi.txt /storage/emulated/0/") i tried this because the way you get permission for root looks like this Runtime.getRuntime().exec("su") so naturally i thought this would work but it does not.

Please post if you have any solutions. And pardon my English

Gaurav
  • 3
  • 1
  • you want to copy a file, so you should use the "cp" command instead of "mv". The "mv" command moves the file, while "cp" creates a copy. please don't forget upvote :) – pouya Jun 07 '23 at 12:52
  • 1
    also, this snippest might be useful: `Process process = Runtime.getRuntime().exec(new String[]{"su", "-c", "cp /storage/emulated/0/super/hi.txt /storage/emulated/0/"});` – pouya Jun 07 '23 at 12:53
  • thanks @pouya the solution you provided did work. Here is the kotlin equivalent to that java code `Runtime.getRuntime().exec(arrayOf("su", "-c","cp /storage/emulated/0/super/hi.txt /storage/emulated/0/" ) )` – Gaurav Jun 10 '23 at 05:15

1 Answers1

-3

To execute a command with root privileges on Android, you need to use the "su" command followed by the actual command you want to execute. In your case, you want to copy a file, so you should use the "cp" command instead of "mv". The "mv" command moves the file, while "cp" creates a copy. Here's how you can modify your code to copy the file with root privileges:

try {
    Process process = Runtime.getRuntime().exec(new String[]{"su", "-c", "cp /storage/emulated/0/super/hi.txt /storage/emulated/0/"});
    process.waitFor();
} catch (IOException | InterruptedException e) {
    e.printStackTrace();
}

This code snippet first requests root access using "su" and then executes the "cp" command to copy the file. The "-c" flag is used to pass the command as a single argument to the shell. Please note that this approach requires a rooted device, and your app must have the necessary permissions to access the file system. Additionally, using root access in your app can pose security risks, so make sure to handle it carefully and inform your users about the implications.

Update:

Kotlin implementation as provided by @ᘜᗩᑌᖇᗩᐯ:

Runtime.getRuntime().exec(arrayOf("su", "-c","cp /storage/emulated/0/super/hi.txt /storage/emulated/0/" ) )
pouya
  • 71
  • 8