【问题标题】:How to get images from google drive into android application?如何从谷歌驱动器获取图像到安卓应用程序?
【发布时间】:2016-03-22 12:26:43
【问题描述】:

我有下载图片的代码

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

【问题讨论】:

    标签: java android google-drive-api


    【解决方案1】:

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

    一旦你有了 DriveFile,你就可以使用 open() 和代码来获取文件内容的 InputStream 作为位图

    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编程新手,所以请帮我从谷歌驱动器下载图像提前谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多