问题描述
我的应用在Google云端硬盘上进行了基于云端的备份。备份顺利进行。
我使用以下代码进行备份:
void saveToDrive ){
//上传文件的代码
DriveId mDriveId = metadataResult.getMetadata()。getDriveId();
SharedPreferences sharedPreferences = getApplicationContext()。getSharedPreferences(getApplicationContext()。getPackageName(),MODE_PRIVATE);
sharedPreferences.edit()。putString(DriveId,mDriveId.encodeToString())。apply();
....
}
正如你所看到的,我保存了当我备份时, DriveId
到 SharedPreferences
。因此,使用 DriveId
,我可以恢复为:
void readFromDrive DriveId mDriveId){// DriveId在应用重新安装后不可用!
//下载文件并恢复的代码
}
我的恢复操作需要一个 DriveId
来下载备份文件。我想在应用重新安装时触发恢复 。但是,我没有能够获取 DriveId
,以便我可以下载所需的文件。
我该怎么做?请帮我解决一些问题。
谢谢。
我通过使用代码首先查询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(),查询)
.setResultCallback(metadataCallback);
}
final private ResultCallback< DriveApi.MetadataBufferResult> metadataCallback =
new ResultCallback< DriveApi.MetadataBufferResult>(){
@Override
public void onResult(DriveApi.MetadataBufferResult result){
if(!result.getStatus()。isSuccess ()){
showMessage(检索结果时出现问题);
return;
}
System.out.println(SUCCESS !!);
MetadataBuffer mdb;
mdb = result.getMetadataBuffer(); (元数据md:mdb){
System.out.println(md.getDriveId()。encodeToString());
}
}
};
$ / code>
上面的代码给我返回了 DriveId
。然后使用 DriveId
我使用了上面问题中发布的函数 readFromDrive(driveId)
。
有效!
感谢各位的帮助。
My app takes a cloud-based backup on 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. So using that DriveId
, I can restore as:
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. 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.
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:
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
. And then using that DriveId
I used the function readFromDrive(driveId)
as posted in the question above.
It works!!
Thanks guys for all your help.
这篇关于Android:获取DriveId进行还原的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!