Restore From SD Card - Android Hardware

Android examples for Hardware:SD Card

Description

Restore From 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 RestoreFromSDCard(String privateDBPath) {
        File sd = Environment.getExternalStorageDirectory();
        File currentDB = new File(privateDBPath);
        String dbFileName = currentDB.getName();
        File backupDB = new File(sd, dbFileName);
        return CopyDatabase(backupDB, currentDB);
    }//from  w w w  .j a  v a 2s. com

    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