谷歌推动Android API - 下载的驱动数据库文件 [英] Google Drive Android api - Downloading db file from drive

查看:140
本文介绍了谷歌推动Android API - 下载的驱动数据库文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是下面的code下载已经上传的SQLite数据库文件从谷歌驱动器的数据/数据​​/包名称/数据库文件夹,但该方法完成时,我看到记录在数据库损坏警告信息logcat的和所有数据的设备的应用上被覆盖,显示空白,在打开的应用程序。

I am using the below code for downloading an already uploaded sqlite db file from google drive to the data/data/packagename/databases folder, but when the method completes, I am seeing a db corruption warning message logged in logcat and also all the data on the device for the app is overwritten and shows up blank, upon opening the app.

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完成,一个DB贪污显示上LogCat中,并在应用程序的datanase文件(sqlite的DB)的所有现有数据已经​​一去不复返了。

When the method GetFileFromDrive completes, a db corruption shows up on LogCat and all the existing data on the app's datanase file (sqlite db) is gone.

请帮助,因为我已验证该驱动器上载的源码数据库文件是正确的,良好的,通过下载同一个开放它在SQLite的浏览器。这是一个从驱动器不工作的下载。

Please help, as I have verified that the drive uploaded sqlite db file is correct and well formed, by downloading the same and opening it up in Sqlite Browser. It's the download from drive that is not working.

推荐答案

我能够最终解决自己的问题,用 File.open方法 file.openContents >和设置的结果回调是 DriveContents 结果返回,而不是 ContentsResult

I was able to finally fix my own issue, by replacing the file.openContents with file.open and setting a result call back with DriveContents result back rather than ContentsResult.

此外,使用DriveContents设置在同一个(在Mode_Read_Only最初开设在开放式),然后写出来插入数据库路径位置的物理.db的文件。一个InputStream

Also, used the DriveContents to set an InputStream on the same (initially opened in Mode_Read_Only in open method) and then writing it out into a physical ".db" file on the data base path location.

现在,数据库没有遭到损坏,并成功地恢复数据。

Now, the database doesn't get corrupted and restores data successfully.

这篇关于谷歌推动Android API - 下载的驱动数据库文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆