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

老子叫甜甜 提交于 2019-11-26 06:07:29

问题


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.


回答1:


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.




回答2:


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




回答3:


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



回答4:


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



回答5:


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.



来源:https://stackoverflow.com/questions/22295903/how-to-delete-a-file-on-google-drive-using-google-drive-android-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!