Search code examples
javaandroidgoogle-drive-apigoogle-drive-android-api

Convert DriveId to human readable path


In my app, I'm coding an activity to backup the local database to Google Drive. I let the user pick the folder where to save the file using Drive's intentPicker and saving the result in my OnActivityResult:

@Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        switch (requestCode) {
            // REQUEST_CODE_PICKER
            case 2:
                intentPicker = null;

                if (resultCode == RESULT_OK) {
                    //Get the folder's drive id
                    DriveId mFolderDriveId = data.getParcelableExtra(
                            OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);

                    uploadToDrive(mFolderDriveId);
                }

After uploading the file I want to show the user where the file was saved. So, I need to convert folder's DriveId to a human readable path like /drive/MyBackups/May.

I've already tried driveId.toString but it only returns a string with unhelpful numbers and letters like DriveId:CASDFAD2Jmasdf==....


Solution

  • As ago suggested, even if possible, showing the full path of a folder is not expected for an app with Drive FILE scope. So I solved it requesting metadata and showing just the title.

        private void setBackupFolderTitle(DriveId id){
            id.asDriveFolder().getMetadata((mGoogleApiClient)).setResultCallback(
                new ResultCallback<DriveResource.MetadataResult>() {
                    @Override
                    public void onResult(DriveResource.MetadataResult result) {
                        if (!result.getStatus().isSuccess()) {
                            showErrorDialog();
                            return;
                        }
                        Metadata metadata = result.getMetadata();
                        // Set folder title in TextView
                        folderTextView.setText(metadata.getTitle());
                    }
                }
            );
        }