如何删除使用谷歌推动Android API,谷歌驱动器上的文件器上、文件、Android、API

2023-09-11 20:37:04 作者:生活不相信眼泪

我是新来的谷歌推动Android的API,而且我学习它。但是,我遇到了一个问题,那就是使用谷歌推动Android的API,我不能删除一个文件,没有它的一个例子。可以anybood帮我解决这个问题吗?非常感谢。

I'm new to Google Drive Android API, and I'm learning it. But I encountered a problem that is I cannot delete a file using Google Drive Android API, there isn't an example of it. Can anybood help me with this question? Thanks alot.

推荐答案

更新(2015年四月) GDAA终于有它自己的垃圾功能呈现以下无关。答案

UPDATE (April 2015) GDAA finally has it's own 'trash' functionality rendering the answer below IRRELEVANT.

原来的答案: 由于谢丽尔上面提到的,你可以结合这两个API。

ORIGINAL ANSWER: As Cheryl mentioned above, you can combine these two APIs.

下面code段,从在这里拍摄< /一>,展示了如何可以做到的:

The following code snippet, taken from here, shows how it can be done:

第一,获得双方的的 GoogleApiClient 的和的 ... services.drive.Drive 的

First, gain access to both GoogleApiClient, and ...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();
}

二,实现一个RESTful API调用上GDAA的DriveId:

Second, implement RESTful API calls on GDAA's DriveId:

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.

首先,如果试图删除文件创建后立即对 getResourceId()的落在它的脸上,返回的空的。不相关的问题在这里,我要提出一个SO唠叨就可以了。

First, if you try to delete a file immediately after you created it, the getResourceId() falls on it's face, returning null. Not related to the issue here, I'm gonna raise an SO nag on it.

其次,这是一个HACK!,它不应该留在你的code过去GDAA实施垃圾和删除功能。

And second, IT IS A HACK! and it should not stay in your code past GDAA implementation of TRASH and DELETE functionality.