如何将谷歌驱动器中的图像转换为Android应用程序?

时间:2016-03-22 12:26:43

标签: java android google-drive-api

我有下载图片的代码

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

1 个答案:

答案 0 :(得分:0)

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

一旦你有了DriveFile,就可以使用open()和代码(例如

)将一个InputStream作为Bitmap获取到文件的内容
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();
      });