简体   繁体   中英

Downloading Images from Google Drive using 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM