如何将图像从谷歌驱动器获取到Android应用程序? [英] How to get images from google drive into android application?

查看:55
本文介绍了如何将图像从谷歌驱动器获取到Android应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下载图片的代码

private class DownloadImage extends AsyncTask<String, Void, Bitmap>
    {
        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(ClusteringMinimaTest.this);
            // Set progressdialog title
            mProgressDialog.setTitle("Download Image Tutorial");
            // Set progressdialog message
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();
        }

        @Override
        protected Bitmap doInBackground(String... URL) {

            String imageURL = URL[0];

            Bitmap bitmap = null;
            try {
                // Download Image from URL
                InputStream input = new java.net.URL(imageURL).openStream();
                // Decode Bitmap
                bitmap = BitmapFactory.decodeStream(input);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(bitmap!= null)
            {
                //saveToInternalStorage(bitmap);
            }
            return bitmap;
        }
}

推荐答案

假设您有

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,就可以使用open()和诸如以下的代码将InputStream作为位图获取到文件的内容中

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

这篇关于如何将图像从谷歌驱动器获取到Android应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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