How to get Google Drive file ID

旧城冷巷雨未停 提交于 2019-12-28 06:52:06

问题


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

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/25553989/how-to-get-google-drive-file-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!