简体   繁体   中英

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==... .

Working with the Drive API methods for folders, note these important keys given in Drive Rest API - Work with Folders :

  • A folder is a file with the MIME type application/vnd.google-apps.folder and with no extension.
  • You can use the alias root to refer to the root folder anywhere a file ID is provided

With these given keys, you can add in your query the parent ID with reference to the file that you're looking for then you can follow the logic given in this SO post - What's the right way to find files by “full path” in Google Drive API v2 .

I hope it will work for you.

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());
                }
            }
        );
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM