我如何在android中通过命令行复制文件?

时间:2015-04-01 15:07:51

标签: android shell

我想将文件从data/data/mypackage复制到/mnt/sdcard但是当我应用命令行时没有发生任何事情。我的手机已经植根了,我已经渗透了这个命令。我有数组列表并在获取所有命令“getCommandsToExecute()”方法后将所有命令放入此数组中并应用我的所有命令:

public final boolean execute() {
        boolean retval = false;

        try {
            ArrayList<String> commands = getCommandsToExecute();
            if (null != commands && commands.size() > 0) {
                Process suProcess = Runtime.getRuntime().exec("su");

                DataOutputStream os = new DataOutputStream(
                        suProcess.getOutputStream());

                // Execute commands that require root access
                for (String currCommand : commands) {
                    os.writeBytes(currCommand + "\n");
                    os.flush();
                }

                os.writeBytes("exit\n");
                os.flush();

                try {
                    int suProcessRetval = suProcess.waitFor();
                    if (255 != suProcessRetval) {
                        // Root access granted
                        retval = true;
                    } else {
                        // Root access denied
                        retval = false;
                    }
                } catch (Exception ex) {
                    Log.e("ROOT", "Error executing root action", ex);
                }
            }
        } catch (IOException ex) {
            Log.w("ROOT", "Can't get root access", ex);
        } catch (SecurityException ex) {
            Log.w("ROOT", "Can't get root access", ex);
        } catch (Exception ex) {
            Log.w("ROOT", "Error executing internal operation", ex);
        }

        return retval;
    }

我的数组列表中包含以下命令:

ArrayList<String> hi= new ArrayList<String>();
hi.add("shell cp /data/data/com.askfm/databases /mnt/sdcard");

1 个答案:

答案 0 :(得分:1)

您必须使用cat src >dest

开箱即用的Android中没有cp命令。有一些工具,如BusyBox(herethere),但必须单独安装。