【问题标题】:Unable to get DriveID & ResourceID of a just created file无法获取刚刚创建的文件的 DriveID 和 ResourceID
【发布时间】:2014-02-03 17:12:02
【问题描述】:

我一直在按照 Google 的文档在 Google Drive 上创建一个文件夹,然后在其中创建一个 jpeg 文件。 How to create file

然后,我需要生成指向该文件的直接链接。我正在使用以下链接执行此操作:https://docs.google.com/uc?export=download&id=[File ResourceID]

文件在光盘上创建。我可以使用浏览器访问它。 问题是,在 onCreateFile(DriveFolder.DriveFileResult driveFileResult) 回调或其他任何地方,我无法获得正确的 ResourceID。

private void saveFileToDrive(final Bitmap image) {
        // Start by creating a new contents, and setting a callback.
        Log.i(TAG, "Creating new contents.");
        DriveApi.newContents(mGoogleApiClient).addResultCallback(new DriveApi.OnNewContentsCallback() {

            @Override
            public void onNewContents(final DriveApi.ContentsResult result) {
                // If the operation was not successful, we cannot do anything
                // and must fail.
                if (!result.getStatus().isSuccess()) {
                    Log.i(TAG, "Failed to create new contents.");
                    return;
                }
                // Otherwise, we can write our data to the new contents.
                Log.i(TAG, "New contents created. ID:"+result.getContents().getDriveId());
                // Get an output stream for the contents.
                OutputStream outputStream = result.getContents().getOutputStream();
                // Write the bitmap data from it.
                ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
                image.compress(Bitmap.CompressFormat.JPEG, 100, bitmapStream);
                try {
                    outputStream.write(bitmapStream.toByteArray());
                } catch (IOException e1) {
                    Log.i(TAG, "Unable to write file contents.");
                }
                // Create the initial metadata - MIME type and title.
                // Note that the user will be able to change the title later.
                MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
                        .setMimeType("image/jpeg")
                        .setTitle(ds.getFujifilmOrderID()+".jpeg")
                        .build();

                Drive.DriveApi.getFolder(mGoogleApiClient, PhotoPrintOrder.this.MyRealFontFolder).createFile(mGoogleApiClient, metadataChangeSet, result.getContents()).addResultCallback(new DriveFolder.OnCreateFileCallback() {
                    @Override
                    public void onCreateFile(DriveFolder.DriveFileResult driveFileResult) {
                        Log.d(TAG, "DriveID:"+driveFileResult.getDriveFile().getDriveId());
                        Log.d(TAG, "ResourceID:"+driveFileResult.getDriveFile().getDriveId().getResourceId());

                    }
                });
            }
        });
    }

输出是这样的:

DriveID:DriveId:CAESABiGAiCMlafg_VA=

资源ID:空

如果我关闭应用程序然后重新启动它,并列出文件夹的内容,我会得到正确的值:

驱动器ID:CAESHDBCeXR3by1ta0hDQ0JXRzAyWjA5UFIzUmlha0UYhAIgjJWn4P1Q

资源ID:0Bytwo-mkHCCBWG02Z09PR3RiakE

我尝试重新连接 GoogleApiClient、driveFileResult.getDriveFile().commitAndCloseContents()、DriveApi.requestSync()。 对我没有任何帮助。

如果有任何可能的帮助,我将不胜感激。

【问题讨论】:

标签: android google-drive-api


【解决方案1】:

requestSync 异步请求同步并立即返回。它不能保证同步完成,并且在同步完成之前您不能有非空资源 ID。

【讨论】:

    【解决方案2】:

    请注意,.getDriveId() 方法返回的是 DriveId 实例,而不是字符串。当您将此实例与“DriveID:”连接时,它只使用.toString()。也许这不是你想要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-04
      • 1970-01-01
      相关资源
      最近更新 更多