tags:

views:

173

answers:

4

Hello,

I would like to know the best way to copy a file in the filesystem? (android java function )

(sdcard/video/test.3gp -----> sdcard/video_bis/test2.3gp)

Is there an example somewhere?

Regards

+1  A: 

You can copy the file using standard Java I/O streams - there's nothing special you need to do. Here's an example on copying a file. You might want to change the example so it's copying more than 1 byte at a time, though :)

Erich Douglass
+1  A: 

I guess it depends on what you mean by best way of copying the file.

Since the file is on the sdcard you can use the normal java.io-package for reading and writing the file in the new place, as per Erich's answer.

Another option is accessing the shell, which I don't know if it will work, but which might be more efficient, since it uses the underlying system's cp-command.

In this case I assume that commands would only contain something like "cp /sdcard/video/test.3gp /sdcard/video_bis/test2.3gp".

Even if this does work, I expect that this might stop working, since it really seems like a security issue in ways..

Mikael Ohlson
This makes more sense for what I *thought* you were asking about first, i.e. moving files. Perhaps that will teach me to think before answering.
Mikael Ohlson
A: 

You may use

private static final String[] COMMAND = { "dd", "if=/sdcard/video/test.3gp", "of=/sdcard/video_bis/test2.3gp", "bs=1024" };

// ...

try {
    final Process pr = Runtime.getRuntime().exec(COMMAND);
    final int retval = pr.waitFor();
    if ( retval != 0 ) {
        System.err.println("Error:" + retval);
    }
}
catch (Exception e) {
    // TODO: handle exception
}

Works on the emulator, you should check if it works on your phone.

dtmilano