Trash, Delete in new Google Drive Android API?

后端 未结 7 650
余生分开走
余生分开走 2020-11-29 11:54

UPDATE (May 2015):
the \'trash\' functionality has been implemented in GDAA, making the question below irrelevant.

ORIGINAL QUESTION:

相关标签:
7条回答
  • 2020-11-29 11:58

    As of Google Play services 7.5, both delete and trash are supported: https://developers.google.com/drive/release-notes#drive_android_api_google_play_services_75_-_may_28th_2015

    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.

    0 讨论(0)
  • 2020-11-29 12:00

    If you are working with App Folder (invisible to user), you goal to delete the file is only to save user space and never pick it again. So, you can use the "empty and forget" strategy.

    From @Mark Carter (because the Android Drive API changed):

        private void deleteContents(final DriveFile driveFile) {
        driveFile.open(this.client, DriveFile.MODE_WRITE_ONLY, null).setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
            @Override
            public void onResult(DriveApi.DriveContentsResult result) {
                if (!result.getStatus().isSuccess()) {
                    // oh noes!
                    return;
                }
    
                final DriveContents contents = result.getDriveContents();                
    
                try {
                    MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder().setTitle("deleted").build();
                    contents.commit(PhotoAdapter.this.client, metadataChangeSet).setResultCallback(new ResultCallback<Status>() {
                        @Override
                        public void onResult(Status status) {
                            if (!status.isSuccess()) {
                                // more oh noes!
                                return;
                            }
                            // nicely marked, now destroy data
                            OutputStream out = contents.getOutputStream();
                            try {
                                out.write(0x0);
                                //nicely empty
                            } catch (IOException e) {
                                //oh, no...
                                throw new RuntimeException(e);
                            }finally {
                                try {
                                    out.close();
                                } catch (IOException e) {
                                    //what we can do? Just log it
                                }
                            }
                        }
                    });
                }
                catch (Exception e) {
                    //Unsuccessful. Log it, rollback  contents and cry
                    contents.discard(PhotoAdapter.this.client);
                }
            }
    
        });
    }
    
    0 讨论(0)
  • 2020-11-29 12:00

    The new Version of the Google Play Services (7.0.0 / March 2015) finally features DriveRessource.trash(). See https://developer.android.com/reference/com/google/android/gms/drive/DriveResource.html

    Haven't tested it yet - I'll report back soon.

    // edit Well, I tested it and it does work ... but not for files within the app folder: Cannot trash App Folder or files inside the App Folder. Apparently you cannot use this feature whilst using the app folder.

    0 讨论(0)
  • 2020-11-29 12:01

    If you want to have a full control over synchronization, do not use Google Drive Android NEW API (at least for now). It is not good enough yet (October 2014) or respectively it works in different way than "real time" OLD API. Main problems are, that you can't delete file, changes are not real-time, metadata are often cached a lot (when I make a search query, in results I can see deleted files even after many hours!). Probably due to some optimizations drive services runs when they want and how they want, so almost nothing is under your control - you never know how drive service uses cache, and you can't force drive service to "do the job right now because I need it".

    Oh, and another disadvantage is, that code is much more complicated that same logic created in OLD API :)

    0 讨论(0)
  • 2020-11-29 12:07

    While Google Drive Android API does not yet support delete, you can delete the contents (and, at the same time, rename the title, so that you can ignore it in future). This might be useful to devs using the AppFolder.

    private void deleteContents(final DriveFile driveFile) {
      driveFile.open(mGoogleApiClient, DriveFile.MODE_WRITE_ONLY, null).setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
        @Override
        public void onResult(DriveContentsResult result) {
          if (!result.getStatus().isSuccess()) {
            // oh noes!
            return;
          }
          DriveContents contents = result.getDriveContents();
          try {
            MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder().setTitle(DELETED_DRIVE_TITLE).build();
            contents.commit(mGoogleApiClient, metadataChangeSet).setResultCallback(
                    new ResultCallback<Status>() {
                      @Override
                      public void onResult(Status status) {
                        if (!status.isSuccess()) {
                          // more oh noes!
                          return;
                        }
                        // nicely deleted
                      }
                    });
          }
          catch (Exception e) {
            contents.discard(mGoogleApiClient);
          }
        }
      });
    }
    
    0 讨论(0)
  • 2020-11-29 12:13

    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.

    0 讨论(0)
提交回复
热议问题