0

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

pinoyyid
  • 21,499
  • 14
  • 64
  • 115
Paolo Rotolo
  • 1,243
  • 1
  • 14
  • 22
  • 1
    When you get the mFolderDriveId, I'd suggest you make a metadata fetch request and show just the folder name to the user. Since the user has just selected the folder by navigating through his drive he would already have enough context. To get the full path you need to have access to the ancestors of the selected folder all the way up to root which is not expected for an app with Drive FILE scope. – Shailendra May 16 '16 at 00:38

2 Answers2

2

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.

Community
  • 1
  • 1
Teyam
  • 7,686
  • 3
  • 15
  • 22
0

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());
                }
            }
        );
    }
Paolo Rotolo
  • 1,243
  • 1
  • 14
  • 22