【问题标题】:Google Drive image upload from android从 android 上传 Google Drive 图片
【发布时间】:2017-07-26 19:06:14
【问题描述】:

我正在尝试使用 API 将图像上传到谷歌驱动器。我已经搜索了很多,但我没有找到方法。我有一个上传文本文件的演示代码,它可以工作。我需要更改它以上传图像。 这是代码...

public void CreateFileOnGoogleDrive(DriveContentsResult result){


    final DriveContents driveContents = result.getDriveContents();

    // Perform I/O off the UI thread.
    new Thread() {
        @Override
        public void run() {
            // write content to DriveContents
            OutputStream outputStream = driveContents.getOutputStream();
            Writer writer = new OutputStreamWriter(outputStream);
            try {
                writer.write("Hello abhay!");
                writer.close();
            } catch (IOException e) {
                Log.e(TAG, e.getMessage());
            }

            MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                    .setTitle("abhaytest2")
                    .setMimeType("text/plain")
                    .setStarred(true).build();

            // create a file in root folder
            Drive.DriveApi.getRootFolder(mGoogleApiClient)
                    .createFile(mGoogleApiClient, changeSet, driveContents)
                    .setResultCallback(fileCallback);
        }
    }.start();
}

如何更改此代码以从文件上传图像。(通过设备中图像的给定位置)。? 我发现了一些教程,但这些都是不推荐使用的方法。

【问题讨论】:

    标签: android google-drive-api google-drive-android-api


    【解决方案1】:

    使用此代码将图像上传到谷歌驱动器...

    new Thread() {
                @Override
                public void run() {
                    // write content to DriveContents
                    OutputStream outputStream = driveContents.getOutputStream();
                    // Write the bitmap data from it.
                    MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
                            .setMimeType("image/jpeg").setTitle(title)
                            .build();
                    Bitmap image = BitmapFactory.decodeFile(location));
                    ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
                    image.compress(Bitmap.CompressFormat.JPEG, 80, bitmapStream);
                    try {
                        outputStream.write(bitmapStream.toByteArray());
                    } catch (IOException e1) {
                        Log.i("E", "Unable to write file contents.");
                    }
                    image.recycle();
                    outputStream = null;
                    String title = "noisy";
    
                    Log.i("E", "Creating new pic on Drive (" + title + ")");
                    Drive.DriveApi.getFolder(mGoogleApiClient,driveId)
                            .createFile(mGoogleApiClient, metadataChangeSet, driveContents)
                            .setResultCallback(fileCallback);
                }
            }.start();
    

    【讨论】:

      【解决方案2】:

      首先,您无需创建文件,然后将内容写入其中。

      private void saveFiletoDrive(final File file, final String mime) {
          Drive.DriveApi.newDriveContents(mDriveClient).setResultCallback(
                  new ResultCallback<DriveContentsResult>() {
                      @Override
                      public void onResult(DriveContentsResult result) {
                          if (!result.getStatus().isSuccess()) {
                              Log.i(TAG, "Failed to create new contents.");
                              return;
                          }
                           Log.i(TAG, "Connection successful, creating new contents...");
                          OutputStream outputStream = result.getDriveContents()
                                  .getOutputStream();
                          FileInputStream fis;
                          try {
                              fis = new FileInputStream(file.getPath());
                              ByteArrayOutputStream baos = new ByteArrayOutputStream();
                              byte[] buf = new byte[1024];
                              int n;
                              while (-1 != (n = fis.read(buf)))
                                  baos.write(buf, 0, n);
                              byte[] photoBytes = baos.toByteArray();
                              outputStream.write(photoBytes);
      
                              outputStream.close();
                              outputStream = null;
                              fis.close();
                              fis = null;
      
                          } catch (FileNotFoundException e) {
                              Log.w(TAG, "FileNotFoundException: " + e.getMessage());
                          } catch (IOException e1) {
                              Log.w(TAG, "Unable to write file contents." + e1.getMessage());
                          }
      
                          String title = file.getName();
                          MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
                                  .setMimeType(mime).setTitle(title).build();
      
                          if (mime.equals(MIME_PHOTO)) {
                              if (VERBOSE)
                                  Log.i(TAG, "Creating new photo on Drive (" + title
                                          + ")");
                              Drive.DriveApi.getFolder(mDriveClient,
                                      mPicFolderDriveId).createFile(mDriveClient,
                                      metadataChangeSet,
                                      result.getDriveContents());
                          } else if (mime.equals(MIME_VIDEO)) {
                              Log.i(TAG, "Creating new video on Drive (" + title
                                      + ")");
                              Drive.DriveApi.getFolder(mDriveClient,
                                      mVidFolderDriveId).createFile(mDriveClient,
                                      metadataChangeSet,
                                      result.getDriveContents());
                          }
      
                          if (file.delete()) {
                              if (VERBOSE)
                                  Log.d(TAG, "Deleted " + file.getName() + " from sdcard");
                          } else {
                              Log.w(TAG, "Failed to delete " + file.getName() + " from sdcard");
                          }
                      }
                  });
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-10
        • 2019-02-16
        • 2022-08-20
        相关资源
        最近更新 更多