简体   繁体   English

如何使用Google Drive Android API删除Google Drive上的文件

[英]How to delete a file on google drive using Google Drive Android API

I'm new to Google Drive Android API, and I'm learning it. 我是Google Drive Android API的新手,正在学习中。 But I encountered a problem that is I cannot delete a file using Google Drive Android API, there isn't an example of it. 但是我遇到一个问题,就是我无法使用Google Drive Android API删除文件,没有示例。 Can anybood help me with this question? anybood可以帮我解决这个问题吗? Thanks alot. 非常感谢。

UPDATE (April 2015) 更新(2015年4月)
GDAA finally has it's own ' trash ' functionality rendering the answer below IRRELEVANT. GDAA终于有了自己的“ 垃圾回收 ”功能,使答案在IRRELEVANT下面。

ORIGINAL ANSWER: 原始答案:
As Cheryl mentioned above, you can combine these two APIs. 如上文Cheryl所述,您可以结合使用这两个API。

The following code snippet, taken from here , shows how it can be done: 此处获取的以下代码段显示了如何实现:

First, gain access to both GoogleApiClient , and ...services.drive.Drive 首先,访问GoogleApiClient... services.drive.Drive

GoogleApiClient _gac;
com.google.api.services.drive.Drive _drvSvc;

public void init(MainActivity ctx, String email){
  // build GDAA  GoogleApiClient
  _gac = new GoogleApiClient.Builder(ctx).addApi(com.google.android.gms.drive.Drive.API)
        .addScope(com.google.android.gms.drive.Drive.SCOPE_FILE).setAccountName(email)
        .addConnectionCallbacks(ctx).addOnConnectionFailedListener(ctx).build();

  // build RESTFul (DriveSDKv2) service to fall back to for DELETE
  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).build();
}

Second, implement RESTful API calls on GDAA's DriveId: 其次,在GDAA的DriveId上实现RESTful API调用:

public void trash(DriveId dId) {
  try {
    String fileID =  dId.getResourceId();
      if (fileID != null)
        _drvSvc.files().trash(fileID).execute();
  } catch (Exception e) {} 
}

public void delete(DriveId dId) {
  try {
    String fileID = dId.getResourceId();
      if (fileID != null)
        _drvSvc.files().delete(fileID).execute();
  } catch (Exception e) {} 
}

... and voila, you are deleting your files. ...然后瞧,您正在删除文件。 And as usual, not without problems. 和往常一样,并非没有问题。

First, if you try to delete a file immediately after you created it, the getResourceId() falls on it's face, returning null . 首先,如果您尝试在创建文件后立即删除它,则getResourceId()会落在文件的表面上,并返回null Not related to the issue here, I'm gonna raise an SO nag on it. 与这里的问题无关,我要对此提出一个建议。

And second, IT IS A HACK! 其次, 这是一个骗局! and it should not stay in your code past GDAA implementation of TRASH and DELETE functionality. 并且它不应留在代码中,超过GDAA实现的TRASH和DELETE功能。

File deletion is not yet supported. 尚不支持文件删除。 You can always fall back to using the RESTful API for things like this. 您总是可以退回使用RESTful API进行此类操作。

To delete you can use the following code. 要删除,您可以使用以下代码。 Then use createFile to copy a new file on drive. 然后使用createFile复制驱动器上的新文件。

    private void deleteFile(DriveFile file) {
        // [START drive_android_delete_file]
        getDriveResourceClient()
            .delete(file)
            .addOnSuccessListener(this, aVoid -> {
                Log.e(TAG, "File Deleted");
            })
            .addOnFailureListener(this, e -> {
                Log.e(TAG, "Unable to delete file", e);
                showMessage(getString(R.string.delete_failed));
            });
    }

https://developers.google.com/drive/v2/reference/files/delete https://developers.google.com/drive/v2/reference/files/delete

You need the file-id to delete the file and the instance of the service: 您需要file-id来删除文件和服务实例:

import com.google.api.services.drive.Drive;

... 

private static void deleteFile(Drive service, String fileId) {
    try {
      service.files().delete(fileId).execute();
    } catch (IOException e) {
      System.out.println("An error occurred: " + e);
    }
  }

Delete is supported by the Google Drive Android API as of Google Play services 7.5 using the DriveResource.delete() method. 从Google Play服务7.5开始,Google Drive Android API使用DriveResource.delete()方法支持删除。

We recommend using trash for user visible files rather than delete, to give users the opportunity to restore any accidentally trashed content. 我们建议对用户可见的文件使用废纸rather,而不是删除,以便使用户有机会还原任何意外的废纸content。 Delete is permanent, and recommended only for App Folder content, where trash is not available. 删除是永久性的,建议仅在没有垃圾箱的应用文件夹中使用。

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

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