No shell commands are part of the SDK, so this is not supported.  Even if
you get this to work, you are likely to break on different devices.

The correct way to do this is to use the File etc APIs to implement your
copying.  For example, this is some code in the platform:

    // copy a file from srcFile to destFile, return true if succeed, return
    // false if fail
    public static boolean copyFile(File srcFile, File destFile) {
        boolean result = false;
        try {
            InputStream in = new FileInputStream(srcFile);
            try {
                result = copyToFile(in, destFile);
            } finally  {
                in.close();
            }
        } catch (IOException e) {
            result = false;
        }
        return result;
    }

    /**
     * Copy data from a source stream to destFile.
     * Return true if succeed, return false if failed.
     */
    public static boolean copyToFile(InputStream inputStream, File
destFile) {
        try {
            if (destFile.exists()) {
                destFile.delete();
            }
            FileOutputStream out = new FileOutputStream(destFile);
            try {
                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) >= 0) {
                    out.write(buffer, 0, bytesRead);
                }
            } finally {
                out.flush();
                try {
                    out.getFD().sync();
                } catch (IOException e) {
                }
                out.close();
            }
            return true;
        } catch (IOException e) {
            return false;
        }
    }


On Wed, Mar 21, 2012 at 10:38 AM, A B <[email protected]> wrote:

>  I want to copy file from on directory to another in my native C program.
> I tried using system function but it's not working.
>
> I tried this code in native code of Android
>
> int result = system("cp /mnt/test /Download/"); // It's not working
>
>  This system function returns 256 (error code) integer value. So we can
> say system function is working in Android. I also installed BusyBox so I
> can use the cp command also.
>
> If I execute directly cp /mnt/test /Download/ command then it's working
> fine.
>
> So what is problem here in system function. Have I missed something?
>
> Thanks
>
> Sam Jona
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to 
> android-developers@​googlegroups.com<[email protected]>
> To unsubscribe from this group, send email to
> android-developers+​[email protected]<android-developers%[email protected]>
> For more options, visit this group at
> http://groups.google.com/​group/android-developers?hl=en<http://groups.google.com/group/android-developers?hl=en>




-- 
Dianne Hackborn
Android framework engineer
[email protected]

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to