2

I've created a file to Drive root using Google Drive Android API. How can I get this file ID to share it using Google Client Library?

Getting DriveFileResult in ResultCallback<DriveFileResult> callback returns Null:

String fileId = result.getDriveFile().getDriveId().getResourceId();
Konstantin Konopko
  • 5,229
  • 4
  • 36
  • 62

2 Answers2

3

The callback is to the file being created locally. The DriveId will only have a resourceId when the file is synced to the server. Until then getResourceId will return null.

https://developer.android.com/reference/com/google/android/gms/drive/DriveId.html#getResourceId()

Use CompletionEvents to be notified when syncing with the server has occurred. Then calling getResourceId() should deliver what you are expecting.

Arthur Thompson
  • 9,087
  • 4
  • 29
  • 33
  • Thank you, but could you suggest how to do this sync? – Konstantin Konopko Aug 29 '14 at 11:02
  • Listen, my file was created in Drive and I saw it. Does it mean file already synced? – Konstantin Konopko Aug 29 '14 at 11:37
  • 2
    You don't have to do anything to sync, it will happen automatically. You can listen for change events to find out when the resource changes, including adding the resourceId: https://developers.google.com/drive/android/events – Cheryl Simon Aug 29 '14 at 15:14
  • @CherylSimon thank you! using change event listener I've got an Id of file `ChangeEvent.getDriveId().getResourceId()` and it seems to work. Now trying insert a new permission of that file. – Konstantin Konopko Sep 01 '14 at 12:32
  • 1
    Up-to-date and correct resolution of this problem can be found [here](http://stackoverflow.com/questions/22874657/unpredictable-result-of-driveid-getresourceid-in-google-drive-android-api/31553269#31553269). – seanpj Aug 28 '15 at 13:52
0

this code might help to identify the id for a file present in the google drive.

    public static void main(String[] args) throws IOException {
    // Build a new authorized API client service.
    Drive service = getDriveService();

    // Print the names and IDs for up to 10 files.
    FileList result = service.files().list()
         .setMaxResults(10)
         .execute();
    List<File> files = result.getItems();
    if (files == null || files.size() == 0) {
        System.out.println("No files found.");
    } else {
        System.out.println("Files:");
        for (File file : files) {
            System.out.printf("%s (%s)\n", file.getTitle(), file.getId());
        }
    }
}
Samson Sunny
  • 121
  • 1
  • 3
  • Unfortunately, you are mixing the [REST](https://developers.google.com/drive/web/about-sdk) Api with the [GDAA](https://developers.google.com/drive/android/intro). Not a good idea, It has been fully answered [here already](http://stackoverflow.com/questions/22874657/unpredictable-result-of-driveid-getresourceid-in-google-drive-android-api/31553269#31553269) – seanpj Aug 28 '15 at 13:29