3

i'm trying to get an app started so I can backup my apps to sdcard. I ran:

        Process p = Runtime.getRuntime().exec("su");
    Process c = Runtime.getRuntime().exec(
            "cp /data/app/com.ebay.mobile-1.apk"
                    + Environment.getExternalStorageDirectory()
                    + "/ebay.apk");
    Log.d("copy", "done");

When it runs I can grant super user then it says it's done but there is no file is not copied. I'm just using the eBay app as an example and test.

Thanks for any help.

Fix: thanks for the help below but i found a full fix and kinda different way of doing things here "http://stackoverflow.com/questions/10735273/copy-folders-in-data-data-to-sdcard-viceversa?rq=1"

user1056798
  • 245
  • 6
  • 20

2 Answers2

7

Android doesn't support cp command. For this you have to either use cat command (from shell only)like,

cat source_file > dest_file

Or use BusyBox with your application.

EDIT:

also you can use command,

dd if=source_file of=dest_file
user370305
  • 108,599
  • 23
  • 164
  • 151
  • More info http://stackoverflow.com/questions/4714411/how-to-copy-and-edit-files-in-android-shell – user370305 Jul 03 '12 at 16:18
  • Note you don't actually need root, provided the application has the external storage permission. But cat source > dest trick is not a process you can execute, but rather something that has to be interpreted by a shell. It would be probably be preferable to read the file and write it back out using a while loop in java. – Chris Stratton Jul 03 '12 at 17:09
  • @ChrisStratton - If you want to install busybox and use command shell then only need root permission, else other options works with only WRITE_EXTERNAL_STORAGE permission in application's manifest file. – user370305 Jul 03 '12 at 17:13
  • I've tried both your suggestions but couldn't get them to work, there no errors and it says its done but nothing actually happened. Do you have any examples? – user1056798 Jul 03 '12 at 17:15
  • @ChrisStratton - cat command works because the phone has a full-featured shell which supports stream redirection. – user370305 Jul 03 '12 at 17:16
  • @user370305 No, you do not need root to install busybox; you only need root to install it in particular places. The cat command **WILL NOT WORK** for this usage, because no shell is being invoked by the asker's program. They would have to exec the shell instead, and pass the compound command to it. – Chris Stratton Jul 03 '12 at 17:18
  • @user1056798 - Did you mentioned "android.permission.WRITE_EXTERNAL_STORAGE" in application's manifest file. – user370305 Jul 03 '12 at 17:19
  • @user1056798 If you do a search you will find many examples on how to copy a file with java. – Chris Stratton Jul 03 '12 at 17:20
  • @user370305 From a **shell** yes, but the asker is not running a shell. Try the posted code snippet with your 'cat source_file > dest_file' - it will not work, as there is no shell to interpret the meaning of '>' – Chris Stratton Jul 03 '12 at 17:24
  • @ChrisStratton - Yes, you are right, As In my answer I just give a alternative to CP command, but I forgot to mentioned its for shell. Anyway I edited it. Thanks. – user370305 Jul 03 '12 at 18:12
0

This QA helped me out a lot, so I wanted to add something to the various exec answers. Rather than using Runtime.getRuntime().exec(String), consider using Runtime.getRuntime().exec(String[]). This should prevent file paths with spaces from wreaking havoc on your filesystem.

Try

Process copy = Runtime.getRuntime().exec(
new String[]{
    "dd", 
    "if=" + inputFile.getAbsolutePath(), 
    "of=" + outputFile.getAbsolutePath()
});
dhakim
  • 508
  • 3
  • 10