2016-02-26 27 views
0

现在我做了记录加速器,磁铁场,陀螺仪(50赫兹)的磨损应用,并将其写入到一个.db文件(即使用SQLiteOpenHelper包含3个表(加速,磁铁场,陀螺仪)。将整个.db文件从Android Wear发送到智能手机的最佳方式是什么?

每天一次,我想.db文件发送给它的手持设备。

我认为这是最好的使用DataApi,我试图通过SQLite的这个代码(https://gist.github.com/tajchert/dc30560891bc6aee76fb)取代境界。

在此代码,我认为这部分

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(); 
} 

应该改变这样的

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(); 
    } 

,但我不知道如何转换(字节[]字节组)到.db文件的掌上电脑。 (什么是“TOFILE”下面的替代功能。这个代码也是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()); 
     } 
    } 

请帮帮我!

回答

0

基本上,该步骤执行此如下:

  1. 访问掌上电脑上的原始数据库文件(您的原始问题有这部分权利)
  2. 将该文件读入字节数组(Elegant way to read file into byte[] array in Java,等等)
  3. 发送的ByteArray使用Data API(可能是作为一种资产来穿:http://developer.android.com/training/wearables/data-layer/assets.html
  4. 保存该字节组回到上磨损的数据库路径(很多这种在线的例子,这里有一个:http://www.java2s.com/Code/Java/File-Input-Output/Readfiletobytearrayandsavebytearraytofile.htm

没有必要处理中间文件。

+0

我真的很感激你的温柔帮助! 现在我会根据你的指示尝试! – LittleWat

+0

我可以做到!谢谢!! – LittleWat

0

您可以保存您的数据库这样

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) { 
    } 

然后你可以的backupdb文件发送到您的磨损设备

+0

像这样硬编码数据库路径是非常糟糕的做法;不能保证任何给定的设备(或未来的Android版本)都会将其数据库保存在同一个位置。为什么不使用API​​提供的调用:'SQLiteDatabase.getReadableDatabase()。getPath()'? – String

+0

谢谢! FileChannel似乎对复制.db文件非常有帮助。 但实际上我想知道如何将android Wear的backupDB文件发送到智能手机以及如何在智能手机中接收db文件。 如果您知道,我期待您的回答。 – LittleWat

+1

如果要恢复磨损设备中的数据库,可以简单地将dst.transferFrom(src,0,src.size())调用更改为dst.transferTo(src,0,dst.size());这将恢复数据库。至于发送数据库,您可以简单地使用蓝牙实现该 –

相关问题