1

I am using google drive api contents given on GitHub for selecting folder. With below code , I could able to get folder id to transfer data but I m not able to get folder name or folder path. Can some one help over this? I tried to use asDriveFolder but did not get required info.

Here is code to get drive Id of selected folder.

public class GoogleFolderSelector extends GoogleDriveBaseActivity {


    String TAG = "Google Folder Picker";

    private static final int REQUEST_CODE_OPENER = 1;

    @Override
    public void onConnected(Bundle connectionHint) {
        super.onConnected(connectionHint);
        IntentSender intentSender = Drive.DriveApi
                .newOpenFileActivityBuilder()
                .setMimeType(new String[]{DriveFolder.MIME_TYPE})
                .build(getGoogleApiClient());
        try {
            startIntentSenderForResult(
                    intentSender, REQUEST_CODE_OPENER, null, 0, 0, 0);
        } catch (IntentSender.SendIntentException e) {
            Log.w(TAG, "Unable to send intent", e);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        try {
            switch (requestCode) {
                case REQUEST_CODE_OPENER:
                    if (resultCode == RESULT_OK) {
                        DriveId driveId = (DriveId) data.getParcelableExtra(
                                OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
                        DriveFolder driveFolder = driveId.asDriveFolder();

                        showMessage("Folder Path"+ driveFolder);

                        showMessage("Selected folder's ID: " + driveId);

                    }
                    finish();
                    break;
                default:
                    super.onActivityResult(requestCode, resultCode, data);
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
            Popup.longpopup("Connection Established, Click to select Folder", this);
        }
    }
}
Panache
  • 1,701
  • 3
  • 19
  • 33
  • Try doing it in the Drive REST API, since GDAA will give you only the files, folders created by your Android App. You can check this related [SO post](https://stackoverflow.com/a/31457960/5995040), to help you with implementing REST API in android. Also take note that there is no real path for drive since there can be multiple parents for a Drive ID. Hope this helps. – Mr.Rebot Oct 14 '17 at 16:01
  • will explore, thanks – Panache Oct 15 '17 at 05:36

1 Answers1

0

In order to get folder name, let use meta data as below

Task<Metadata> getMetadataTask = getDriveResourceClient().getMetadata(file);
getMetadataTask
        .addOnSuccessListener(this,
                metadata -> {
                    showMessage(getString(
                            R.string.metadata_retrieved, metadata.getTitle()));
                    finish();
                })
        .addOnFailureListener(this, e -> {
            Log.e(TAG, "Unable to retrieve metadata", e);
            showMessage(getString(R.string.read_failed));
            finish();
        });

Here is the full instruction

For the folder path, I used to try to retrieve but it seems it is not necessary as you could manipulate everything with driveId such as add file into this driveId folder or create another folder inside this driveId folder. Explore google sample app will give you a better sight.

thanhbinh84
  • 17,876
  • 6
  • 62
  • 69