Running a shell command to copy files from Android App

大城市里の小女人 提交于 2019-12-07 12:44:44

问题


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"


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/11314821/running-a-shell-command-to-copy-files-from-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!