3

I've been following Google's documentation to create a Folder on a Google Drive and then create a jpeg file inside it. How to create file

Then, I need to generate a direct link to that file. I'm doing this by using the following link: https://docs.google.com/uc?export=download&id=[File ResourceID]

The file is created on the disc. I can access it using browser. The problem is, that in onCreateFile(DriveFolder.DriveFileResult driveFileResult) callback or anywhere else, I'm unable to get correct 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());

                    }
                });
            }
        });
    }

The output is like this:

DriveID:DriveId:CAESABiGAiCMlafg_VA=

ResourceID: null

If I close application and then start it again, and will list content of the folder, I get correct values:

DriveID:CAESHDBCeXR3by1ta0hDQ0JXRzAyWjA5UFIzUmlha0UYhAIgjJWn4P1Q

ResourceID: 0Bytwo-mkHCCBWG02Z09PR3RiakE

I tried reconnecting the GoogleApiClient, driveFileResult.getDriveFile().commitAndCloseContents(), DriveApi.requestSync(). Nothing did the trick for me.

I would appreciate any possible help.

Alex
  • 31
  • 3

2 Answers2

0

requestSync requests a sync asynchronously and returns immediately. It doesn't guarantee that the sync is done, and you can't have non-null resource IDs before the sync is done.

Burcu Dogan
  • 9,153
  • 4
  • 34
  • 34
0

Note that the .getDriveId() method is returning a DriveId instance, not a String. And when you concatenate this instance with "DriveID:" it just uses .toString(). Perhaps this is not what you want.

PFROLIM
  • 1,194
  • 9
  • 11