将整个 .db 文件从 android wear 发送到智能手机的最佳方法是什么?
What is the best way to send whole .db file from android wear to smartphone?
现在我正在制作一个记录加速器、磁场、陀螺仪 (50Hz) 的磨损应用程序,并使用 SQLiteOpenHelper 将它们写入 .db 文件(包含 3 个表(加速度、磁场、陀螺仪)。
每天一次,我想将.db文件发送到它的掌上电脑。
我认为最好使用DataApi,我尝试在这段代码中用SQLite替换Realm(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();
}
但我不知道如何在手持设备中将 (byte [] byteArray) 转换为 .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());
}
}
请帮帮我!!!
您可以像这样保存您的数据库
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文件发送到你的穿戴设备
基本上,执行此操作的步骤如下:
- 访问手持设备上的原始数据库文件(你原来的问题这部分是正确的)
- 将该文件读入字节数组(Elegant way to read file into byte[] array in Java,等等)
- 使用数据 API 将 ByteArray 发送到 Wear(可能作为资产:http://developer.android.com/training/wearables/data-layer/assets.html)
- 将 ByteArray 保存回 Wear 上的数据库路径(网上有很多这样的例子,这里是一个:http://www.java2s.com/Code/Java/File-Input-Output/Readfiletobytearrayandsavebytearraytofile.htm)
无需处理中间文件。
现在我正在制作一个记录加速器、磁场、陀螺仪 (50Hz) 的磨损应用程序,并使用 SQLiteOpenHelper 将它们写入 .db 文件(包含 3 个表(加速度、磁场、陀螺仪)。
每天一次,我想将.db文件发送到它的掌上电脑。
我认为最好使用DataApi,我尝试在这段代码中用SQLite替换Realm(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();
}
但我不知道如何在手持设备中将 (byte [] byteArray) 转换为 .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());
}
}
请帮帮我!!!
您可以像这样保存您的数据库
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文件发送到你的穿戴设备
基本上,执行此操作的步骤如下:
- 访问手持设备上的原始数据库文件(你原来的问题这部分是正确的)
- 将该文件读入字节数组(Elegant way to read file into byte[] array in Java,等等)
- 使用数据 API 将 ByteArray 发送到 Wear(可能作为资产:http://developer.android.com/training/wearables/data-layer/assets.html)
- 将 ByteArray 保存回 Wear 上的数据库路径(网上有很多这样的例子,这里是一个:http://www.java2s.com/Code/Java/File-Input-Output/Readfiletobytearrayandsavebytearraytofile.htm)
无需处理中间文件。