Downloading Images from Google Drive using Google Drive Android API?

南笙酒味 提交于 2020-01-05 14:11:30

问题


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:


Assuming you have a DriveId from when you inserted the image, you can then retrieve the file using Drive.DriveApi.getFile() - this returns you a DriveFile.

Once you have a DriveFile, you can get an InputStream to the contents of the file as a Bitmap using open() and code such as

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


来源:https://stackoverflow.com/questions/31689077/downloading-images-from-google-drive-using-google-drive-android-api

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