Google Drive: The file size is null

2019-04-08 12:26发布

问题:

when I try to get the file size by getFileSize() on Google Drive the result is null. I know that the result can be null when the file in not stored on Drive but the file is stored on Drive (downloadUrl is different from null).

    Drive lService = new Builder(new NetHttpTransport(), new JacksonFactory(), lCredential).setApplicationName("TODO").build();
    FileList files = lService.files().list().execute();

    List<FileDTO> lResult = new ArrayList<FileDTO>();
    for (File lFile : files.getItems()) {
        DriveFileDTO lFileDTO = new DriveFileDTO();
        lFileDTO.setName(lFile.getTitle());
        lFileDTO.setMimeType(lFile.getMimeType());
        lFileDTO.setSize(lFile.getFileSize()); // <-- FileSize = NULL

        lFileDTO.setUrl(lFile.getDownloadUrl());

        lResult.add(lFileDTO);
    }

Can you help me?

Thanks! aGO!

回答1:

Call getHeaders().GetContentLength() on the HttpResponse Class (code assumes myurl is your url):

HttpResponse resp = service.getRequestFactory().buildGetRequest(new GenericUrl(myurl)).execute();
Log.v("abc","file size is : " + resp.getHeaders().getContentLength());


回答2:

A default request for a file list from Google Drive will include file size, but only for files where the file size is not implicitly null. For example, folders and Google spreadsheets (which are simply links) will be null.

This can be verified with the Drive API exlporer. Simply request filesize and mimtype in the fields editor. A key-value pair will not be included in the response for the file types mentioned, even though a filesize was requested -- yet will be returned for other types.



回答3:

Please try reproducing the issue with the OAuth 2.0 Playground:

https://developers.google.com/oauthplayground

By using the OAuth Playground we can send raw requests and check whether there's an issue in the API. I tried reproducing it myself and it worked correctly.