繁体   English   中英

无法获取刚刚创建的文件的DriveID和ResourceID

[英]Unable to get DriveID & ResourceID of a just created file

我一直在关注Google的文档,在Google云端硬盘上创建一个文件夹,然后在其中创建一个jpeg文件。 如何创建文件

然后,我需要生成到该文件的直接链接。 我是通过以下链接执行此操作的: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());

                    }
                });
            }
        });
    }

输出是这样的:

驱动器Id:驱动器Id:CAESABiGAiCMlafg_VA =

ResourceID:null

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

驱动器Id:CAESHDBCeXR3by1ta0hDQ0JXRzAyWjA5UFIzUmlha0UYhAIgjJWn4P1Q

ResourceID:0Bytwo-mkHCCBWG02Z09PR3RiakE

我尝试重新连接GoogleApiClient,driveFileResult.getDriveFile()。commitAndCloseContents(),DriveApi.requestSync()。 对我来说没有什么可以做的。

我很感激任何可能的帮助。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM