jeudi 22 octobre 2015

Android - Create Multiple Exported Databases And Allow User To Select Which One To Import

I have the following code which is used to create a backup of the SQLite database;

    File sd = Environment.getExternalStorageDirectory();
    File data = Environment.getDataDirectory();
    FileChannel source = null;
    FileChannel destination = null;
    String currentDBPath = "/data/com.sjhdevelopment.shaunharrison.myejuiceapp/databases/EJuiceData.db";
    String backupDBPath = "EJuiceData.db";


    File currentDB = new File(data,currentDBPath);
    File backupDB = new File(sd,backupDBPath);

    try
    {
        source = new FileInputStream(currentDB).getChannel();
        destination = new FileOutputStream(backupDB).getChannel();
        destination.transferFrom(source,0,source.size());
        source.close();
        destination.close();
        Toast.makeText(this, "DB Exported", Toast.LENGTH_LONG).show();
    }
    catch(Exception e)
    {
        showError("Error", e.getMessage());
    }

And I have the following code to Import A SQLite Database

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

 if(sd.canWrite())
 {
   String currentDBPath="/data/com.sjhdevelopment.shaunharrison.myejuiceapp/databases/EJuiceData.db";
   String backupDBPath = "EJuiceData.db";
   File backupDB = new File (data,currentDBPath);
   File currentDB = new File(sd, backupDBPath);

   try {
         FileChannel src =new FileInputStream(currentDB).getChannel();
         FileChannel dst = new FileOutputStream(backupDB).getChannel();
         dst.transferFrom(src, 0, src.size());
         src.close();
         dst.close();
         Toast.makeText(getBaseContext(), "DB Imported", Toast.LENGTH_LONG).show();
       }
   catch(Exception e)
   {
         showError("Error", e.getMessage());
   }

My problem is that I now want the user to be able to make multiple backups instead of just the one, I know how I can get todays date via the following;

    Calendar calendar = Calendar.getInstance();
    Date today = calendar.getTime();
    DateFormat dateFormat = new SimpleDateFormat("dd/MMM/yyyy");
    String todayAsString = dateFormat.format(today);

But where in the code export code does todayAsString go?

Also when it comes to importing how do I allow the user to browse their files to select which backup to import?

Thanks

Aucun commentaire:

Enregistrer un commentaire