3

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.

Tony Lin
  • 765
  • 3
  • 15
  • 35

5 Answers5

5

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.

seanpj
  • 6,735
  • 2
  • 33
  • 54
  • I should add, there is another problem with this hack. After trashing/deleting a file/folder this way, it will show as VALID/ALIVE if you address it through GDAA (querying, using it's Resource ID, ...) because it is cached by Google Play Services. As a result, your app may be writing to a file / creating objects in a folder that does not exist anymore. And the 'refresh' delay may be considerably long (hours). – seanpj Aug 18 '14 at 12:31
  • Does this still work? Where are GoogleAccountCredential? and AndroidHttp? Thank you – Nabin Feb 25 '15 at 07:46
  • 1
    It does, but be careful about the latency. I managed to delete the files / folders in RESTful and GDAA did not give me any warning after that so I could write into trashed / nonexistent files, or even crete new files in deleted folder. There are 2 levels of deletion in RESful (or Drive web), TRASH ans DELETE. Neither is reported accurately in GDAA. You may actually plug the code from here into this demo on Github (https://github.com/seanpjanson/GDAADemo) and play with it to see the pitfalls. The credential / authorization stuff is there es well ((MainActivity). See SO 28439129. – seanpj Feb 25 '15 at 14:11
  • Email please, if possible :-) Thank you for the information. – Nabin Feb 26 '15 at 01:01
  • Please see one of the issue that I have started in github – Nabin Feb 26 '15 at 04:17
  • how to setup "_drvSvc"? (could you provide me dependency name?, I did follow https://developers.google.com/drive/v3/web/manage-downloads) – Amitabha Biswas Dec 01 '16 at 08:11
1

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);
    }
  }
Alexander_Winter
  • 314
  • 1
  • 3
  • 9
  • Is this example compatible with this example? https://github.com/googledrive/android-demos/blob/master/src/com/google/android/gms/drive/sample/demo/CreateFileActivity.java – Tony Lin Mar 10 '14 at 09:18
  • It should, because you just need the id of the file to delete it and in you get the id in your example. – Alexander_Winter Mar 10 '14 at 09:27
  • @Alexander_Winter I know that but how to construct a object 'device' while i am using the Google Drive Android API ways to Authorization? `mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Drive.API) .addScope(Drive.SCOPE_FILE) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build();` – Tony Lin Mar 10 '14 at 09:40
  • Actually this answer is correct as well, except it does not mention how to get the 'fileId'. It has to come from DriveId.getResourceId() as mentioned in my answer here. – seanpj Mar 15 '14 at 14:03
1

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

Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
1

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));
            });
    }
Christophe Quintard
  • 1,858
  • 1
  • 22
  • 30
0

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.

Daniel
  • 1,239
  • 1
  • 13
  • 24