选择要恢复数据库的文件 android

Pick file to restore database android

我有一个带有数据库的应用程序。我有一个像这样备份数据库的方法:

// Method To Backup Database//
public void OnClick_Backup(View v) {

    // Vibrates For 50 Mill//
    vibe.vibrate(50);

    // Get Calendar Instance//
    Calendar c = Calendar.getInstance();

    // Get Date//
    SimpleDateFormat mFormatter = new SimpleDateFormat("MM-dd-yy");

    // Create App Folder//
    File sd = new File(Environment.getExternalStorageDirectory()+"/C.S. Tracker Backups");
    if(!sd.exists()){
        sd.mkdirs();
    }
        try {
            File sd2 = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();

            // Backup File//
            if (sd2.canWrite()) {
                String currentDBPath = "//data//jordanzimmittidevelopers.com.communityservicelogger//databases//community_service_Database";
                String backupDBPath = "/C.S. Tracker Backups/" + mFormatter.format(c.getTime());
                File currentDB = new File(data, currentDBPath);
                File backupDB = new File(sd2, backupDBPath);

                // Replace File If It has Same Name//
                if (currentDB.exists()) {
                    FileChannel src = new FileInputStream(currentDB).getChannel();
                    FileChannel dst = new FileOutputStream(backupDB).getChannel();
                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();
                    Toast.makeText(getApplicationContext(), "Backup is successful to SD card", Toast.LENGTH_SHORT).show();
                }
            }
        } catch (Exception ignored) {
        }
    }

另一种恢复数据库的方法是这样的:

// Method To Restore Database//
public void OnClick_Restore(View v) {

    // Vibrates For 50 Mill//
    vibe.vibrate(50);

    // Get Calendar Instance//
    Calendar c = Calendar.getInstance();

    // Get Date//
    SimpleDateFormat mFormatter = new SimpleDateFormat("MM-dd-yy");

    try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//jordanzimmittidevelopers.com.communityservicelogger//databases//community_service_Database";
            String backupDBPath = "/C.S. Tracker Backups/" + mFormatter.format(c.getTime());
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, backupDBPath);

            if (currentDB.exists()) {
                FileChannel src = new FileInputStream(backupDB).getChannel();
                FileChannel dst = new FileOutputStream(currentDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
                Toast.makeText(getApplicationContext(), "Database Restored successfully", Toast.LENGTH_SHORT).show();
            }
        }
    } catch (Exception ignored) {
    }
}

问题是我有它,所以当用户点击备份时,它会在 phone 上保存一个文件,其中包含备份日期。因此,如果有多个备份,我希望他们能够在弹出框或其他内容中单击他们想要恢复的备份。谢谢

编辑:澄清我该怎么做=所以如果有多个备份,我希望他们能够在弹出框或其他东西中单击他们想要恢复的备份。

 String backupDBPath = here get full path from filepicker; 

 restore (backupDBPath);

这是你的功能:

 public boolean restore(String backupDBPath) {

    File backupDB  = new File( backupDBPath);
    if (!backupDB.exists())
{
          Toast.makeText(getApplicationContext()
    , "file not found:\n"+backupDB.getAbsolutePath()
    , Toast.LENGTH_SHORT).show();

return false;
}

   File currentDB = getDatabasePath("community_service_Database");
   if (!currentDB.exists())
{
          Toast.makeText(getApplicationContext()
    , "file not found:\n"+currentDB.getAbsolutePath()
    , Toast.LENGTH_SHORT).show();

return false;
}

   try {

            FileChannel src = new FileInputStream(backupDB).getChannel();
            FileChannel dst = new FileOutputStream(currentDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
            Toast.makeText(getApplicationContext(), "Database Restored successfully", Toast.LENGTH_SHORT).show();

        return true;

  } catch (Exception e) {
           Toast.makeText(getApplicationContext(), "Exception:\n"+e.getMessage()(), Toast.LENGTH_SHORT).show();

return false;
}

}