我正在使用android-demos来实现android中的Google Drive集成。我成功地在google drive中创建了一个文件。现在我想删除这个新创建的文件。我通过https://developers.google.com/drive/v2/reference/files/delete找到了这方面的参考,现在这个链接中的文件()方法在驱动器中找不到。
private static void deleteFile(Drive service, String fileId) {
try {
service.files().delete(fileId).execute();
} catch (IOException e) {
System.out.println("An error occurred: " + e);
}
}
现在请告诉我如何从Google Drive中删除文件。我对此做了研究,但没有找到解决办法。有人说使用以前的api的谷歌驱动器。但现在已经过时了。现在google使用V2作为驱动器
上次我检查GDAA中没有删除。参见如何使用google drive Android API删除google drive上的文件
你可以等待它被实现,或者使用REST API https://developers.google.com/drive/v2/reference/files/delete
我怀疑你混淆了两个不同的api。GDAA是一个纯粹的本地API。你的应用程序正在与Android Drive应用程序通信。使用REST API,你的应用程序正在通过http与Google Drive服务器通信。你的应用可以使用其中之一,或者两者的混合(尽管你需要非常绝望地这样做)。
目前还不支持使用核心API从Google drive删除文件。因此必须使用Restful API调用。要执行restful API调用,需要将以下jar文件添加到lib文件夹
google-api-client-1.19.1.jar
google-api-client-android-1.19.1.jar
google-api-services-drive-v2-rev158-1.19.1.jar
google-http-client-1.19.0.jar
google-http-client-android-1.19.0.jar
google-http-client-gson-1.19.0.jar
google-oauth-client-1.19.0.jar
gson-2.1.jar
jsr305-1.3.9.jar
现在您可以在核心API调用中执行restful API调用,如下所示
com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential crd = GoogleAccountCredential
.usingOAuth2(
ctx,
Arrays.asList(com.google.api.services.drive.DriveScopes.DRIVE_FILE));
crd.setSelectedAccountName(email);
_drvSvc = new com.google.api.services.drive.Drive.Builder(
AndroidHttp.newCompatibleTransport(), new GsonFactory(), crd).setApplicationName("SmsAndCallLogBackup")
.build();
记住我连接到使用核心API的谷歌驱动器。对于删除,我使用restful API
下面的方法是用来删除文件从谷歌驱动器
public void delete(DriveId dId) {
try {
String fileID = dId.getResourceId();
if (fileID != null)
_drvSvc.files().delete(fileID).execute();
} catch (Exception e) {
e.printStackTrace();
}
}
在异步任务中调用此方法,否则会给出错误它一定会成功的
我已经成功完成了* *
* *private GoogleApiClient api;
public void Update(DriveId dId) {
try {
DriveFile sumFile = dId.asDriveFile();
com.google.android.gms.common.api.Status deleteStatus =
sumFile.delete(api).await();
if (!deleteStatus.isSuccess()) {
Log.e(TAG, "Unable to delete app data.");
} else {
// Remove stored DriveId.
preferences_driverId.edit().remove("drive_id").apply();
}
} catch (Exception e) {
e.printStackTrace();
}
}