Working with file and folder metadata

Files and folders contain metadata that describes them, which includes:

  • The title of the file or folder
  • The MIME type of the file or folder
  • Whether the file or folder is starred
  • Whether the file is editable
  • Whether the file or folder is trashed

Metadata is encapsulated in the Metadata class. The metadata is fetched for a DriveResource by calling the DriveResourceClient.getMetadata method.

Task<Metadata> getMetadataTask = getDriveResourceClient().getMetadata(file);
getMetadataTask
        .addOnSuccessListener(this,
                new OnSuccessListener<Metadata>() {
                    @Override
                    public void onSuccess(Metadata metadata) {
                        showMessage(getString(
                                R.string.metadata_retrieved, metadata.getTitle()));
                        finish();
                    }
                })
        .addOnFailureListener(this, new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.e(TAG, "Unable to retrieve metadata", e);
                showMessage(getString(R.string.read_failed));
                finish();
            }
        });

The metadata may be set or changed when creating or updating a file by using a MetadataChangeSet, setting the appropriate values, and then calling the DriveResourceClient.updateMetadata method.

MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                                      .setStarred(true)
                                      .setIndexableText("Description about the file")
                                      .setTitle("A new title")
                                      .build();
Task<Metadata> updateMetadataTask =
        getDriveResourceClient().updateMetadata(file, changeSet);
updateMetadataTask
        .addOnSuccessListener(this,
                new OnSuccessListener<Metadata>() {
                    @Override
                    public void onSuccess(Metadata metadata) {
                        showMessage(getString(R.string.metadata_updated));
                        finish();
                    }
                })
        .addOnFailureListener(this, new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.e(TAG, "Unable to update metadata", e);
                showMessage(getString(R.string.update_failed));
                finish();
            }
        });

You can also update the metadata with the file contents to commit concurrent or related changes atomically. For example, if a file's metadata also describes the contents, this avoids issues with inconsistent data if the metadata and contents are updated in separate requests and one of those requests fails.

Custom properties

Custom properties allow arbitrary metadata to be associated with a DriveResource in the form of key-value pairs. You can set custom properties as public so that other applications can see them or as private so that only the application that created the custom property can see it. You can also query files, filtering on custom properties.

The following table lists the limits to adding custom properties to a resource:

Limit per resource Maximum
Public custom properties 30
Private custom properties per app 30
Total custom properties 100
Custom property length (key + value) 124 bytes (UTF-8 encoding)

Insert or update custom properties

When inserting or updating a custom property, you must create a CustomPropertyKey that identifies the custom property. You define a CustomPropertyKey by its custom property name and its visibility, public or private. You can then use the MetadataChangeSet.Builder.setCustomProperty method with the CustomPropertyKey and the intended value of the custom property.

CustomPropertyKey approvalPropertyKey =
        new CustomPropertyKey("approved", CustomPropertyKey.PUBLIC);
CustomPropertyKey submitPropertyKey =
        new CustomPropertyKey("submitted", CustomPropertyKey.PUBLIC);
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                                      .setCustomProperty(approvalPropertyKey, "yes")
                                      .setCustomProperty(submitPropertyKey, "no")
                                      .build();
Task<Metadata> updateMetadataTask =
        getDriveResourceClient().updateMetadata(file, changeSet);
updateMetadataTask
        .addOnSuccessListener(this,
                new OnSuccessListener<Metadata>() {
                    @Override
                    public void onSuccess(Metadata metadata) {
                        showMessage(getString(R.string.custom_property_updated));
                        finish();
                    }
                })
        .addOnFailureListener(this, new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.e(TAG, "Unable to update metadata", e);
                showMessage(getString(R.string.update_failed));
                finish();
            }
        });

Delete custom properties

You can delete a custom property by using the MetadataChangeSet.Builder.deleteCustomProperty method and passing it the appropriate CustomPropertyKey. Your application can delete any custom property visible to it, such as public custom properties and private custom properties created by your application.

CustomPropertyKey approvalPropertyKey =
        new CustomPropertyKey("approved", CustomPropertyKey.PUBLIC);
CustomPropertyKey submitPropertyKey =
        new CustomPropertyKey("submitted", CustomPropertyKey.PUBLIC);
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                                      .deleteCustomProperty(approvalPropertyKey)
                                      .deleteCustomProperty(submitPropertyKey)
                                      .build();
Task<Metadata> updateMetadataTask =
        getDriveResourceClient().updateMetadata(file, changeSet);
updateMetadataTask
        .addOnSuccessListener(this,
                new OnSuccessListener<Metadata>() {
                    @Override
                    public void onSuccess(Metadata metadata) {
                        showMessage(getString(R.string.custom_property_deleted));
                        finish();
                    }
                })
        .addOnFailureListener(this, new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.e(TAG, "Unable to update metadata", e);
                showMessage(getString(R.string.update_failed));
                finish();
            }
        });

Enviar comentarios sobre…

Drive API for Android
Drive API for Android