user1056798
user1056798

Reputation: 235

Running a shell command to copy files from Android App

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"

Upvotes: 3

Views: 9474

Answers (2)

dhakim
dhakim

Reputation: 508

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

Upvotes: 0

user370305
user370305

Reputation: 109257

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

Upvotes: 7

Related Questions