Search code examples
androidbackupmp3android-sdcard

android 1.5, programmatically copy songs form /sdcard/songs to /sdcard/backup


i am using eclipse emulator and i want to copy programmatically some mp3s from /sdcard/songs to /sdcard/backup, is there any way to do so? any help and code snippet is greatly appreciated! thanks! :)


Solution

  • You can try this:

    try {
        File sd = Environment.getExternalStorageDirectory();
    
        if (sd.canWrite()) {
            String sourcePath= "/path/to/source/file.mp3";
            String destinationPath= "/path/to/destination/file.mp3";
            File source= new File(sd, sourcePath);
            File destination= new File(sd, destinationPath);
            if (source.exists()) {
                FileChannel src = new FileInputStream(source).getChannel();
                FileChannel dst = new FileOutputStream(destination).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
            }
    } catch (Exception e) {}