Delete or trash files and folders

You can move a DriveResource, such as file or folder, to or from the user's trash with the DriveResourceClient.trash and DriveResourceClient.untrash methods. These can be used to trash or untrash files or folders owned by the currently authenticated user. DriveResource objects that are shared with but not owned by the currently authenticated user cannot be trashed or untrashed.

If the target of the trash/untrash operation is a folder, all descendants of that folder are similarly trashed or untrashed. If your application does not have permission to all of the descendants of the target folder, the entire operation is canceled. If the folder contains items not owned by the current user, the folder is still trashed or untrashed, but items not owned by the current user are unaffected and remain in their current state.

Check if a file is in the trash

You can determine if a file is already in the trash or can be trashed by retrieving its metadata. There are three methods for determining a file's status:

Metadata.isTrashed()
Returns true if the resource is already in the user's trash.
Metadata.isTrashable()
Returns true if the resource is owned by the currently authenticated user and is not in the AppFolder. The resource may still be untrashable if one of its descendents is not accessible by your application or is in the AppFolder.
Metadata.isExplicitlyTrashed()
Returns true if the resource was explicitly trashed. Returns false if the resource was trashed as a result of its parent folder being trashed.

Trash or untrash a file or folder

The typical workflow to trash or untrash a resource is as follows:

  1. Request the resource metadata.
  2. Check if isTrashable is true.
  3. (Optional) Check if isTrashed is true. Calling trash on an already trashed resource has no effect.
  4. Call DriveResourceClient.trash or DriveResourceClient.untrash as applicable

The following code illustrates the code necessary to check the status of a file and toggle it between the trashed and untrashed state:

if (!metadata.isTrashable()) {
    showMessage(R.string.trashable_error);
    return;
}

DriveResource driveResource = metadata.getDriveId().asDriveResource();
Task<Void> toggleTrashTask;
if (metadata.isTrashed()) {
    toggleTrashTask = mDriveResourceClient.untrash(driveResource);
} else {
    toggleTrashTask = mDriveResourceClient.trash(driveResource);
}
toggleTrashTask = updateUiAfterTask(toggleTrashTask);
handleTaskError(toggleTrashTask, R.string.unexpected_error);

Delete a file

Deleting a file bypasses trash and permanently deletes the file. A file can be deleted using the DriveResourceClient.delete method. To delete a file, the authenticated user must be the owner of the file and your app must have access to the file. If the resource being deleted is a folder, your app must also have access to all descendants of the folder.

The following illustrates the code necessary to delete a file:

getDriveResourceClient()
        .delete(file)
        .addOnSuccessListener(this,
                new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        showMessage(getString(R.string.file_deleted));
                        finish();
                    }
                })
        .addOnFailureListener(this, new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.e(TAG, "Unable to delete file", e);
                showMessage(getString(R.string.delete_failed));
                finish();
            }
        });

发送以下问题的反馈:

此网页
Drive API for Android
Drive API for Android