Downloading Images from Google Drive using Google Drive Android API?

时间:2015-07-28 23:57:54

标签: android image download google-drive-api google-drive-android-api

Basically, I have been using this guide: https://developers.google.com/drive/android/intro

I have gotten the picture to upload into drive with the help of: https://github.com/googledrive/android-demos/tree/master/src/com/google/android/gms/drive/sample/demo

However, the GitHub project linked above does not provide any way to retrieve Images from Drive, it only shows text files. I am aware of the QueryNonTextFilesActivity.java but this gives the result in MetaDataBuffer which is appended onto ResultsAdapter. My question is: how do I use this data and convert to images (.png)?

Thank You

1 个答案:

答案 0 :(得分:5)

假设您在插入图片后有DriveId,则可以使用Drive.DriveApi.getFile()检索文件 - 这会返回DriveFile

获得DriveFile后,您可以使用open()和代码(例如

)获取文件内容InputStream作为位图
file.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null)
    .setResultCallback(
      new ResultCallback<DriveApi.DriveContentsResult>() {
        @Override
        public void onResult(DriveApi.DriveContentsResult result) {
        if (!result.getStatus().isSuccess()) {
          // Handle an error
        }
        DriveContents driveContents = result.getDriveContents();
        InputStream is = driveContents.getInputStream();
        Bitmap bitmap = BitmapFactory.decodeStream(is);
        // Do something with the bitmap

        // Don't forget to close the InputStream
        is.close();
      });