0

I am trying drive api on android with the sample code from the sdk but getting this error. The permissions to access the app folder is granted. I have also tried deleting the hidden data and disconnecting the app from drive and reconnecting back, but of no use. The error remains same. Below is my code:

private void createFileInAppFolder() {
        final Task<DriveFolder> appFolderTask = getDriveResourceClient().getAppFolder();
        final Task<DriveContents> createContentsTask = getDriveResourceClient().createContents();
        Tasks.whenAll(appFolderTask, createContentsTask)
                .continueWithTask(new Continuation<Void, Task<DriveFile>>() {
                    @Override
                    public Task<DriveFile> then(@NonNull Task<Void> task) throws Exception {
                        DriveFolder parent = appFolderTask.getResult();
                        DriveContents contents = createContentsTask.getResult();
                        OutputStream outputStream = contents.getOutputStream();
                        try  {
                            Writer writer = new OutputStreamWriter(outputStream);
                            writer.write("Hello World!");
                        }catch (Exception e){

                        }

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

                        return getDriveResourceClient().createFile(parent, changeSet, contents);
                    }
                })
                .addOnSuccessListener(this,
                        new OnSuccessListener<DriveFile>() {
                            @Override
                            public void onSuccess(DriveFile driveFile) {
                                Log.e("BACKUP","Success"+driveFile.getDriveId().encodeToString());
                                finish();
                            }
                        })
                .addOnFailureListener(this, new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.e("BACKUP", "Unable to create file", e);
                    }
                });
    }

protected void signIn() {
        Set<Scope> requiredScopes = new HashSet<>(2);
        requiredScopes.add(Drive.SCOPE_FILE);
        requiredScopes.add(Drive.SCOPE_APPFOLDER);
        GoogleSignInAccount signInAccount = GoogleSignIn.getLastSignedInAccount(this);
        if (signInAccount != null && signInAccount.getGrantedScopes().containsAll(requiredScopes)) {
            initializeDriveClient(signInAccount);
        } else {
            GoogleSignInOptions signInOptions =
                    new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                            .requestScopes(Drive.SCOPE_FILE)
                            .requestScopes(Drive.SCOPE_APPFOLDER)
                            .build();
            GoogleSignInClient googleSignInClient = GoogleSignIn.getClient(this, signInOptions);
            startActivityForResult(googleSignInClient.getSignInIntent(), REQUEST_CODE_SIGN_IN);
        }
    }

Error log:

E/BACKUP: Unable to create file
                                                                 com.google.android.gms.common.api.ApiException: 1502: Invalid parent folder.
                                                                     at com.google.android.gms.internal.zzbsv.onError(Unknown Source)
                                                                     at com.google.android.gms.internal.zzbql.onTransact(Unknown Source)
                                                                     at android.os.Binder.execTransact(Binder.java:453)

Please help..Thanks in advance

  • 1
    You may refer with this [thread](https://stackoverflow.com/questions/33248420/invalid-parent-folder-error-for-appfolder-in-drive-api). It was suggested to use `resourceID` rather than `DriveID`. See this [reported issue](https://github.com/googledrive/android-demos/issues/35). You may also use the [REST API](https://developers.google.com/drive/v3/web/appdata#working_with_the_application_data_folder) as a workaround. – abielita Jan 16 '18 at 16:19
  • have you finished this how to download file using resourceID – M.Yogeshwaran Apr 19 '18 at 08:09

1 Answers1

0

I have used resourceID rather than DriveID and it worked fine. Thanks all!