How to Delete a File on Google Drive Using Google Drive Android API

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

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.

How to delete file from Google Drive

Last time I checked, there is no delete in GDAA. See How to delete a file on google drive using Google Drive Android API

You can either wait for it to be implemented, or use the REST API https://developers.google.com/drive/v2/reference/files/delete

I suspect you are confusing two different APIs. GDAA is a purely local API, ie. your app is communicating with the Android Drive app. With the REST API, your app is talking over http to the Google Drive servers. Your app can use either, or a mixture of both (although you need to be pretty desparate to do that).

Trash, Delete in new Google Drive Android API?

Google Drive Android API doesn't sync with the remote resources instantly. Depending on the scheduler, it may take a while to sync. The scheduling is dependant to Android's account sync components those are making sure that network bandwidth and battery life is conserved and efficiently used.

Additionally, as of Developer Preview, we don't support deletion or trashing. But, the next release will likely to support these actions.

Google Drive Android API: Deleted folder still exists in query

What you are seeing, is a 'normal' behavior of the GDAA, that can be explained if you look closer at the 'Lifecycle of a Drive file' diagram (warning: I've never seen the source code, just assuming from what I observed).

See, the GDAA, unlike the REST Api, creates a layer that does its best to create caching and network traffic optimization. So, when you manipulate the file/folder from the 'outside' (like the web app), the GDAA layer has no knowledge of the fact until it initiates synchronization, controlled by it's own logic. I myself originally assumed that GooDrive has this under control by dispatching some kind of notification back to the GDAA, but it apparently is not the case. Also, some Googlers mentioned 'requestSync()' as a cure, but I never succeeded to make it work.

What you think you're doing, is polling the GooDrive. But effectively, you're polling the GDAA (local GooPlaySvcs) whose DriveId is still valid (not updated), unlike the real GooDrive object that is already gone.

This is one thing that is not clearly stated in the docs. GDAA is not the best Api for EVERY application. It's caching mechanism is great for transparently managing online/offline states, network traffic optimization. battery life, ... But in your situation, you may be better off by using the REST Api, since the response you get reflects the current GooDrive state.

I myself faced a similar situation and had to switch from the GDAA back to the REST (and replaced polling with a private GCM based notification system). Needless to say, by using the REST Api, your app gets more complex, usually requiring sync adapter / service to do the data synchronization, managing network states, ... all the stuff GDAA gives you for free).

In case you want to play with the 2 apis side-by side, there are two identical CRUD implementation you can use (GDAA, REST) on Github.

Good Luck

Google Drive Rest API v3 - how to move a file to the trash?

I don't see any error with your code on how to move a file to trash wherein you'll use files.update with {'trashed':true}.

Sample code in this thread:

The solution is to create an empty File setting only the new values:

File newContent = new File();
newContent.setTrashed(true);
service.files().update(fileId, newContent).execute();

I have tried this in Google Drive Try it! and successfully moved the file to trash.

Just make sure that the File refers to com.google.api.services.drive.model.File and it is not java.io.File.



Related Topics



Leave a reply



Submit