0

Now I am making an wear app that records accelerator, magnet-field, gyroscope (50Hz) and write them to a .db file (that contains 3 tables(acceleration, magnet-field, gyroscope) using SQLiteOpenHelper.

Once a day, I would like to send the .db file to its handheld.

I think it is the best to use DataApi and I tried to replace Realm by SQLite in this code(https://gist.github.com/tajchert/dc30560891bc6aee76fb).

In this code, I thought this part

public static void syncRealm(Context context){
        File writableFolder = context.getFilesDir();
        File realmFile = new File(writableFolder, Realm.DEFAULT_REALM_NAME);
        Asset realAsset = Tools.assetFromFile(realmFile);
        new FileSender(realAsset, context).execute();
}

should be changed like this

 public static void syncDB(Context context) {
    WearSqliteOpenHelper helper = new WearSqliteOpenHelper(context);
    String dbPath = helper.getReadableDatabase().getPath();
    File dbFile = new File(dbPath);
    Uri dbUri = Uri.fromFile(dbFile);
    Asset realAsset = Asset.createFromUri(dbUri);
    new FileSender(realAsset, context).execute();
  }

but I don't know how to convert (byte [] byteArray) to .db file in handheld. (What is the alternative function of "toFile" below. this code is also in https://gist.github.com/tajchert/dc30560891bc6aee76fb)

   private void toFile(byte [] byteArray){
        File writableFolder = ListenerService.this.getFilesDir();
        File realmFile = new File(writableFolder, Realm.DEFAULT_REALM_NAME);
        if (realmFile.exists()) {
            realmFile.delete();
        }
        try {
            FileOutputStream fos=new FileOutputStream(realmFile.getPath());
            fos.write(byteArray);
            fos.close();
        }
        catch (java.io.IOException e) {
            Log.d(TAG, "toFile exception: " + e.getLocalizedMessage());
        }
    }

Please help me!!!

LittleWat
  • 53
  • 6

2 Answers2

0

You can save your database like this

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

        if (sd.canWrite()) {
            String currentDBPath = "//data//{package name}//databases//{database name}";
            String backupDBPath = "{database name}";
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, backupDBPath);

            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();
            }
        }
    } catch (Exception e) {
    }

Then you can send the backupDB file to your wear device

Niza Siwale
  • 2,390
  • 1
  • 18
  • 20
  • It's really bad practice to hardcode the database path like this; there's no guarantee that any given device (or future Android release) will keep its DB in the same place. Why not use the call that the API provides: `SQLiteDatabase.getReadableDatabase().getPath()`? – Sterling Feb 26 '16 at 17:28
  • Thanks! FileChannel seems to be very helpful to copy .db file. but actually I would like to know how to send the backupDB file of the android wear to smartphone and how to receive the db file in smartphone. If you know, I am looking forward to your answer. – LittleWat Feb 27 '16 at 05:32
  • 1
    If you want to restore the database in the wear device, you can simply change the the dst.transferFrom(src, 0, src.size()) call to dst.transferTo(src,0,dst.size()); this will restore the database. As for sending the database, you can simply use bluetooth to achieve that – Niza Siwale Feb 27 '16 at 10:24
  • Thanks! I know how to copy my db File! Now I have to send my db file of wear to handheld using bluetooth! – LittleWat Mar 01 '16 at 02:06
0

Basically, the steps to do this are as follows:

  1. Access the raw DB file on the handheld (your original question had this part right)
  2. Read that file into a byte array (Elegant way to read file into byte[] array in Java, among others)
  3. Send the ByteArray to Wear using the Data API (probably as an asset: http://developer.android.com/training/wearables/data-layer/assets.html)
  4. Save that ByteArray back into the DB path on Wear (lots of examples of this online, here's one: http://www.java2s.com/Code/Java/File-Input-Output/Readfiletobytearrayandsavebytearraytofile.htm)

There's no need to deal with intermediate files.

Community
  • 1
  • 1
Sterling
  • 6,365
  • 2
  • 32
  • 40