gpt4 book ai didi

android - Google Drive Android api - 从驱动器下载 db 文件

转载 作者:行者123 更新时间:2023-11-30 02:28:58 26 4
gpt4 key购买 nike

我正在使用以下代码将已上传的 sqlite db 文件从谷歌驱动器下载到 data/data/packagename/databases 文件夹,但是当该方法完成时,我看到 logcat 中记录了一条数据库损坏警告消息,并且打开应用程序时,设备上用于该应用程序的所有数据都将被覆盖并显示为空白。

mfile = Drive.DriveApi.getFile(mGoogleApiClient, mResultsAdapter.getItem(0).getDriveId());
mfile.openContents(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null).setResultCallback(contentsOpenedCallback);

--mfile is an instance of DriveFile

final private ResultCallback<ContentsResult> contentsOpenedCallback = new ResultCallback<ContentsResult>()
{
@Override
public void onResult(ContentsResult result)
{
if (!result.getStatus().isSuccess())
{
FileUtils.appendLog(getApplicationContext(), Tag + "-onResult", "Error opening file");
return;
}

try
{
if (GetFileFromDrive(result))
{
//FileUtils.Restore(getApplicationContext());
SharedPrefHelper.EditSharedPreference(getApplicationContext(), Constants.PREFS_DO_RESTORE, false);
}

}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};

private boolean GetFileFromDrive(ContentsResult result)
{
Contents contents = result.getContents();
//InputStreamReader rda = new InputStreamReader(contents.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(contents.getInputStream()));
FileOutputStream outStream;
String currLine;
boolean restoreSuccess = false;

File sourceDbFile = BackupDBBeforeDeletion();

if(sourceDbFile != null)
sourceDbFile.delete();

try
{
outStream = new FileOutputStream(getApplicationContext().getDatabasePath(Constants.DB_NAME));
while ((currLine = reader.readLine()) != null)
{
outStream.write(currLine.getBytes());
}

outStream.flush();

reader.close();
outStream.close();
restoreSuccess = true;
}
catch (FileNotFoundException e)
{
// TODO: Log exception
}
catch (IOException e)
{
// TODO: Log Exception
}

return restoreSuccess;
}

当 GetFileFromDrive 方法完成时,数据库损坏显示在 LogCat 上,应用程序的数据文件 (sqlite db) 上的所有现有数据都消失了。

请帮忙,因为我已经通过下载相同的文件并在 Sqlite 浏览器中打开它来验证驱动器上传的 sqlite db 文件是正确且格式正确的。这是无法正常工作的驱动器下载。

最佳答案

此解决方案对我不起作用。我从谷歌驱动器获得的 sqlite 文件比我在谷歌驱动器中保存的要长。发生这种情况的原因可能是您正在使用 Bufferreader 类和读取 char 中数据的方法 readline。在我的项目和经验中,这个解决方案不起作用,我得到了一个损坏的 sqlite 数据库。所以...我以字节为单位将 sqlite 数据库保存在谷歌驱动器中,所以 **我使用 BufferedInputStream 从谷歌驱动器中以字节为单位读取相同的 sqlite 数据库。这非常适合我:

DriveContents contents = result.getDriveContents();
BufferedInputStream bis = new BufferedInputStream(contents.getInputStream());
byte[] buffer = new byte[1024];
int bytesread = 0;
FileOutputStream outStream;

/*if(currentDB != null)
currentDB.delete();*/

try
{
outStream = new FileOutputStream(currentDB);
while( (bytesread = bis.read(buffer)) != -1)
{
outStream.write(buffer,0,bytesread);
}

outStream.flush();
bis.close();
outStream.close();
}
catch (FileNotFoundException e)
{
Log.i(TAG,e.getMessage());
}
catch (IOException e)
{
Log.i(TAG,e.getMessage());
}
finally {
Toast.makeText(getActivity().getBaseContext(),"Data from Google Drive restored successfully." , Toast.LENGTH_LONG).show();
}
contents.discard(mGoogleApiClient);

关于android - Google Drive Android api - 从驱动器下载 db 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27532869/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com