Konstantin Konopko
Konstantin Konopko

Reputation: 5422

How to get Google Drive file ID

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

Upvotes: 2

Views: 17125

Answers (2)

Arthur Thompson
Arthur Thompson

Reputation: 9225

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.

Upvotes: 3

Samson Sunny
Samson Sunny

Reputation: 121

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

Upvotes: 0

Related Questions