Delete / trash file from android using android.gms

2020-04-19 06:02发布

I need to delete files from Google Drive using com.google.android.gms.drive. From what I've read here and across the web there is no support for file deletion in the "new API". Is that (still) correct? I mean the API isn't that new anymore ...

I also read about the "clear contents and forget"-strategy, but I'd like not to follow that approach.

Part 2 of the question: Given it's still impossible to delete files via the API mentioned above; is there any easy way to combine the REST API w/ the code I've already written? Something like

  1. get token from GoogleApiClient
  2. fire DELETE request w/ token and file id
  3. ???
  4. profit

edit: The new Google Play Services (version 7.0.0 / March 2015) finally features a trash() method. See https://developer.android.com/reference/com/google/android/gms/drive/DriveResource.html for further details. .


edit2: Apparently you cannot use trash() on files from the app folder: Cannot trash App Folder or files inside the App Folder. :((


edit3: As of May 28th, it's now possible to actually delete files.

2条回答
冷血范
2楼-- · 2020-04-19 06:12

UPDATE (May 2015)
Addition of trash / delete functionality to GDAA 7.5 renders the answer below irrelevant.

ORIGINAL ANSWER:

The sequence is:

  1. Get DriveId from GDAA (file or folder)
  2. Get ResourceId from DriveId.getResourceId() (may be null if file/folder is not committed yet)
  3. use ResourceId (rsid) in this REST call:
com.google.api.services.drive.Drive.files().trash(rsid).execute()
com.google.api.services.drive.Drive.files().delete(rsid).execute()
  1. finally realize that you can't do it since you'll see the file in GDAA long after it has been deleted / trashed. You can even write in it, create files in that folder you've just trashed, ... That's why people introduced the "clear contents and forget" strategy nobody likes.

Needless to say, this applies to any delete / trash action you may perform outside of GDAA universe (manually trash/delete using web interface etc...)

So, to wrap it up. 'pinoyyid' is right, each of the APIs is different and the GDAA can not replace the REST (yet). You'll quickly realize it when you try to work a little deeper, like getting thumbnail url, fighting latency issues etc... On the other hand GDAA has advantages, since it can work off-line without your app handling it. When using REST, your app should do all the Drive calls in some kind of sync service to separate your UI actions from network issues (delays, off-line, ...). GDAA handles this for you, but on it's own timing terms. And with no delete functionality.

I put a piece of code on github, that has both these API's used side-by-side and you may use it to play with different scenarios before committing yourself to one of them.

It would be nice to hear clearly from Google what the final strategy is, i.e.

  • Is GDAA going to replace REST one day, after all the REST functionality is in?
  • Will they retire the REST afterwards?

Good Luck

查看更多
来,给爷笑一个
3楼-- · 2020-04-19 06:37

Delete is supported by the Google Drive Android API as of Google Play services 7.5 using the DriveResource.delete() method.

Delete is permanent, and recommended only for App Folder content, where trash is not available.

查看更多
登录 后发表回答