Google drive Android API - Retrieve file id

1

Good afternoon, in the morning I did this question and I managed to save an image. The problem that arises now, is that I need to recover the id that assigns drive to that image, there is a method that I found on the internet that gives me the id, but it is very different from the method that was given to me in my last question:

private void createFile(final String filename) {
    Drive.DriveApi.newDriveContents(apiClient)
        .setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
            @Override
            public void onResult(DriveApi.DriveContentsResult result) {
                if (result.getStatus().isSuccess()) {

                    writeSampleText(result.getDriveContents());

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

                    //Opción 1: Directorio raíz
                    DriveFolder folder = Drive.DriveApi.getRootFolder(apiClient);

                    //Opción 2: Otra carpeta distinta al directorio raiz
                    //DriveFolder folder =
                    //    DriveId.decodeFromString("DriveId:CAESABjKGSD6wKnM7lQoAQ==").asDriveFolder();

                    folder.createFile(apiClient, changeSet, result.getDriveContents())
                        .setResultCallback(new ResultCallback<DriveFolder.DriveFileResult>() {
                            @Override
                            public void onResult(DriveFolder.DriveFileResult result) {
                                if (result.getStatus().isSuccess()) {
                                    Log.i(LOGTAG, "Fichero creado con ID = " + result.getDriveFile().getDriveId());
                                } else {
                                    Log.e(LOGTAG, "Error al crear el fichero");
                                }
                            }
                        });
                } else {
                    Log.e(LOGTAG, "Error al crear DriveContents");
                }
            }
        })};

I got the example from this link . How can I get the id assigned by google drive?

    
asked by L. Ronquillo 13.06.2017 в 20:43
source

1 answer

1

In the question you mention, in my answer I put the official Google code to create a file in Drive. If you analyze it well, you will see that the same code shows, in the end, how to get the id of the newly created file by getDriveId() :

 final private ResultCallback<DriveFileResult> fileCallback = new
            ResultCallback<DriveFileResult>() {
        @Override
        public void onResult(DriveFileResult result) {
            if (!result.getStatus().isSuccess()) {
                showMessage("Error while trying to create the file");
                return;
            }
            showMessage("Created a file with content: " + result.getDriveFile().getDriveId());
        }
    };
    
answered by 14.06.2017 / 02:13
source