Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get the name of a file using the Google Drive API?

Let's say I use the Google Drive API to select a file that I want to open. I have this code to do so:

// Getting the drive ID for the file
        DriveId driveId = (DriveId) data
                .getParcelableExtra(OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);

        // Getting the selected file
        DriveFile googleDriveFile = Drive.DriveApi.getFile(googleApiClient,
                driveId);

        googleDriveFile.open(googleApiClient, DriveFile.MODE_READ_ONLY,
                null).setResultCallback(
                new ResultCallback<DriveContentsResult>() {
                    @Override
                    public void onResult(DriveContentsResult result) {
                        if (result.getStatus().isSuccess()) {

Is there any way for me to get the file name of this file? (Not the drive ID, but its actual name?)

I'm trying to validate the chosen file (by checking its type, which is in the name), and I can't think of a way to do this other than get the name. The type is .cblite, a Couchbase Lite database file. Normally I'd just filter the Drive picker by MIME type, but .cblite (to my knowledge) isn't one of those options. Instead I'm trying to validate by file name.

Is there a way to do this? (Or is there a way to filter MIME types by miscellaneous/unidentified types?)

like image 522
Christina Avatar asked Dec 24 '22 22:12

Christina


1 Answers

Yes, once you have DriveId, you can get metadata. And one of the metadata fields is getTitle() which will get you the file / folder name. Look at this code snippet:

DriveFile googleDriveFile  = Drive.DriveApi.getFile(googleApiClient, driveId);
MetadataResult mdRslt = googleDriveFile .getMetadata(googleApiClient).await();
if (mdRslt != null && mdRslt.getStatus().isSuccess()) {
  mdRslt.getMetadata().getTitle();
}

The 'await' flavor here is used for convenience, it must be run off-ui thread. Or just turn it into a callback style.

Good Luck

like image 140
seanpj Avatar answered Feb 02 '23 05:02

seanpj