Backup To SD Card - Android Hardware

Android examples for Hardware:SD Card

Description

Backup To SD Card

Demo Code


//package com.java2s;
import android.os.Environment;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class Main {
    public static boolean BackupToSDCard(String privateDBPath) {
        File sd = Environment.getExternalStorageDirectory();
        File currentDB = new File(privateDBPath);
        String dbFileName = currentDB.getName();
        File backupDB = new File(sd, dbFileName);
        return CopyDatabase(currentDB, backupDB);
    }//from   w  ww  .j a  v a2  s . co  m

    private static boolean CopyDatabase(File source, File dest) {
        try {
            FileChannel sourceChannel = new FileInputStream(source)
                    .getChannel();
            FileChannel destChannel = new FileOutputStream(dest)
                    .getChannel();
            destChannel
                    .transferFrom(sourceChannel, 0, sourceChannel.size());
            sourceChannel.close();
            destChannel.close();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}

Related Tutorials