简体   繁体   English

Android:获取 DriveId 进行恢复

[英]Android: Get DriveId for restore

My app takes a cloud-based backup on Google Drive.我的应用程序在 Google Drive 上进行了基于云的备份 The backup occurs smoothly.备份顺利进行。

I use the following code for backup:我使用以下代码进行备份:

void saveToDrive() {
    // Code to upload file
    DriveId mDriveId = metadataResult.getMetadata().getDriveId();
    SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(getApplicationContext().getPackageName(), MODE_PRIVATE);
    sharedPreferences.edit().putString("DriveId", mDriveId.encodeToString()).apply();
    ....
}

As you can see, I save the DriveId to SharedPreferences when I backup.如您所见,我在备份时将DriveId保存到SharedPreferences So using that DriveId , I can restore as:所以使用那个DriveId ,我可以恢复为:

void readFromDrive(DriveId mDriveId) { // DriveId is not available after app reinstall!!
    // Code to download file and restore
}

My restore operation requires a DriveId to download the backed-up file.我的恢复操作需要DriveId才能下载备份文件。 I want to trigger a restore when the app is reinstalled .我想在重新安装应用程序时触发恢复。 But, I am not able to get the DriveId so that I may download the required file.但是,我无法获得DriveId以便我可以下载所需的文件。

How do I do that?我怎么做? Please, I am desperate for some help.拜托,我迫切需要一些帮助。

Thanks.谢谢。

I solved it by first querying the contents of Google Drive using the code:我通过首先使用代码查询 Google Drive 的内容来解决它:

public class GDriveQuery extends BaseDemo {
    @Override
    public void onConnected(Bundle connectionHint) {
        super.onConnected(connectionHint);
        Query query = new Query.Builder()
            .addFilter(Filters.eq(SearchableField.TITLE, "field"))
            .addFilter(Filters.not(Filters.eq(SearchableField.TRASHED, true)))
            .build();
        Drive.DriveApi.query(getGoogleApiClient(), query)
            .setResultCallback(metadataCallback);
    }

    final private ResultCallback<DriveApi.MetadataBufferResult> metadataCallback =
        new ResultCallback<DriveApi.MetadataBufferResult>() {
            @Override
            public void onResult(DriveApi.MetadataBufferResult result) {
                if (!result.getStatus().isSuccess()) {
                    showMessage("Problem while retrieving results");
                    return;
                }
                System.out.println("SUCCESS!!");
                MetadataBuffer mdb;
                mdb = result.getMetadataBuffer();
                for (Metadata md : mdb) {
                    System.out.println(md.getDriveId().encodeToString());
                }
            }
        };
}

The above code returned me the DriveId .上面的代码返回给我DriveId And then using that DriveId I used the function readFromDrive(driveId) as posted in the question above.然后使用该DriveId我使用了上面问题中发布的函数readFromDrive(driveId)

It works!!有用!!

Thanks guys for all your help.谢谢大家的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM