简体   繁体   中英

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. 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.

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

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:

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

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.

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.

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

File deletion is not yet supported. You can always fall back to using the RESTful API for things like this.

To delete you can use the following code. Then use createFile to copy a new file on drive.

    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

You need the file-id to delete the file and the instance of the service:

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.

We recommend using trash for user visible files rather than delete, to give users the opportunity to restore any accidentally trashed content. Delete is permanent, and recommended only for App Folder content, where trash is not available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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