Android Google Drive api:1502:无效的父文件夹

时间:2018-01-16 05:45:39

标签: android google-drive-android-api

我正在使用sdk中的示例代码尝试在Android上使用驱动器api但是出现此错误。授予访问app文件夹的权限。我还尝试删除隐藏数据并断开应用程序与驱动器的连接并重新连接,但没有用。错误保持不变。以下是我的代码:

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);
        }
    }

错误日志:

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)

请帮助..提前谢谢

1 个答案:

答案 0 :(得分:0)

我使用的是resourceID而不是DriveID,它工作正常。谢谢大家!