如何使用 Google Drive Android API 删除 Google Drive 上的文件 [英] How to delete a file on google drive using Google Drive Android API

查看:29
本文介绍了如何使用 Google Drive Android API 删除 Google Drive 上的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Google Drive Android API 的新手,我正在学习它.但是我遇到了一个问题,即我无法使用 Google Drive Android API 删除文件,没有示例.任何人都可以帮我解决这个问题吗?非常感谢.

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 年 4 月)
GDAA 终于有了它自己的垃圾"功能,在 IRRELEVANT 下面给出了答案.

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

原始答案:
正如 Cheryl 上面提到的,您可以组合这两个 API.

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

以下代码片段,摘自此处, 展示了它是如何做到的:

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();
}

其次,在GDAA的DriveId上实现RESTful API调用:

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() 会落在它的脸上,返回 null.与此处的问题无关,我将对此提出 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.

第二,这是一个黑客!,它不应该留在你的代码中,超过垃圾和删除功能的 GDAA 实现.

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

这篇关于如何使用 Google Drive Android API 删除 Google Drive 上的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆