使用Google Drive Android API从Google云端硬盘下载图片? [英] Downloading Images from Google Drive using Google Drive Android API?

查看:179
本文介绍了使用Google Drive Android API从Google云端硬盘下载图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我一直在使用本指南: https://developers.google.com/drive / android / intro

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

我已经把照片上传到驱动器的帮助下: https://github.com/googledrive/android-demos/tree/master/ src / com / google / android / gms / drive / sample / demo

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

但是,上面链接的GitHub项目没有提供任何方法来检索驱动器,它只显示文本文件。我知道QueryNonTextFilesActivity.java,但是这会给MetaDataBuffer中的结果添加到ResultsAdapter中。我的问题是:如何使用这些数据并转换为图像(.png)?

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)?

谢谢

推荐答案

假设你有一个 DriveId ,然后您可以使用 Drive.DriveApi.getFile() - 这将返回一个 DriveFile

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.

一旦你有一个 DriveFile ,你可以得到一个 InputStream 作为Bitmap使用 open()和代码如

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

这篇关于使用Google Drive Android API从Google云端硬盘下载图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆