2

データをSDカードに保存/コピーするコードは次のとおりです。[バックアップ]ボタンをクリックすると、データベースがSDカードのNOTEITディレクトリに保存されます。復元ボタンをクリックすると、このデータベースをデフォルトのディレクトリに復元したいので、誰でもわかりますこれを達成する方法は?

public static void backupDatabase() throws IOException 
{
    try
    {
    File dbFile = new File(Environment.getDataDirectory() + "/data/com.neelrazin.noteit/databases/data");

        File exportDir = new File(Environment.getExternalStorageDirectory()+"/NOTEIT");

        if (!exportDir.exists()) 
        {
            exportDir.mkdirs();
        }

        File file = new File(exportDir, dbFile.getName());

        file.createNewFile();

        FileChannel inChannel = new FileInputStream(dbFile).getChannel();  //fails here

        FileChannel outChannel = new FileOutputStream(file).getChannel();

        try 
        {
            inChannel.transferTo(0, inChannel.size(), outChannel);
        } 
        finally 
        {
            if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
4

1 に答える 1