0

I'm trying to use the Google Drive API to allow the user to select a file and (so far) get the metadata of it.

Here's where I'm at (extracted):

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

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case REQUEST_CODE_OPENER:
            if (resultCode == RESULT_OK) {
                if (data != null) {
                    DriveId driveId = data.getParcelableExtra(OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
                    Log.d(TAG, "driveId = " + driveId.encodeToString());
                    //driveId.asDriveResource().getMetadata(getGoogleApiClient())
                    driveId.asDriveFile().getMetadata(getGoogleApiClient())
                        .setResultCallback(new ResultCallback<DriveResource.MetadataResult>() {
                            public void onResult(@NonNull DriveResource.MetadataResult mdr) {
                                // Never gets here
                            }
                        });
                }
            }
            break;
        default:
            super.onActivityResult(requestCode, resultCode, data);
    }
}

Seems to work just fine. It logs the DriveId it finds and gets to the getMetadata() call. But it never calls the callback: onResult() is never reached.

I feel like I'm almost certainly doing something obvious wrong, but in studying the API documentation and the Google example code, I can't spot what it is.

Edited to add:

I tried changing it from using setResultCallback() to await() (in a thread), but while the DriveID is again fine, the MetadataResult has a statusCode of CANCELED (even though I click SELECT from the Drive file picker). So calling getMetadata() on it returns null.

Does this suggest further where things might be going wrong?

KT_
  • 978
  • 11
  • 26

1 Answers1

0

check getMetadata().setResultCallBack() code here

In the code above, you have kept the onResult block empty. You should use the MetaDataResult obj in onResult block to get metadata about the file/folder you want.

Check for status if success or not on MetaDataResult obj

 final ResultCallback<MetadataResult> metadataCallback = new ResultCallback<MetadataResult>() {
    @Override
    public void onResult(MetadataResult result) {
        if (!result.getStatus().isSuccess()) {
            showMessage("Problem while trying to retrieve the file metadata");
            return;
        }
        if (result.getMetadata().isPinnable()) {
            showMessage("File is pinnable");
        }
};

For more on MetaData see android doc


its the driveId thats not matching if its metadata callback is not a success

try the methods in this SO thread to get the correct id for that file link

u can do this on the driveId u get:

Drive.DriveApi.fetchDriveId(getGoogleApiClient(), driveId.getResourceId()).setResultCallback(idCallback);

final ResultCallback<DriveApi.DriveIdResult> idCallback = new ResultCallback<DriveApi.DriveIdResult>() {
    @Override
    public void onResult(DriveApi.DriveIdResult result) {
        if (!result.getStatus().isSuccess()) {
            showMessage("Cannot find DriveId. Are you authorized to view this file?");
            return;
        }
        DriveId driveId = result.getDriveId();
        showLogDebug(driveId + " in idCallback of fetchId");
        DriveFile file = driveId.asDriveFile();
        file.getMetadata(getGoogleApiClient()).setResultCallback(metadatacallBack);

} };

Community
  • 1
  • 1
AndroidLearner
  • 741
  • 6
  • 11
  • Thanks. Sorry to not be clearer: I removed the actual `onResult` code for brevity of the example and replaced it with that comment, since execution never got there anywhere. In the example I gave, were I to put your code there, it would never get to checking `isSuccess()`. – KT_ Apr 14 '17 at 17:34