如何删除使用谷歌推动Android API,谷歌驱动器上的文件 [英] How to delete a file on google drive using Google Drive Android API

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

问题描述

我是新来的谷歌推动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.

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

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