如何在google驱动器中的文件夹中创建文件?

时间:2017-07-05 13:18:35

标签: android google-drive-api google-drive-android-api

我在创建文件夹内的文件时面临问题,我在google驱动器中创建了单独的文件和单独的文件夹,但我无法在google驱动器中的文件夹中创建文件

我已经使用了用于创建google驱动器文件夹内的文件发布在

下面
public void onClickFolderInRootFlder(View view)
{

    MetadataChangeSet changeSet = new MetadataChangeSet.Builder().setTitle("New CallLog folders").build();
    Drive.DriveApi.getRootFolder(mGoogleApiClient).createFolder(
            mGoogleApiClient, changeSet).setResultCallback(callback);
}

final ResultCallback<DriveFolder.DriveFolderResult> callback = new ResultCallback<DriveFolder.DriveFolderResult>() {
    @Override
    public void onResult(DriveFolder.DriveFolderResult result) {
        if (!result.getStatus().isSuccess()) {
            Toast.makeText(MainActivity.this,"Error while trying to create the folder",Toast.LENGTH_SHORT).show();
            return;
        }
        Toast.makeText(MainActivity.this,"Created a folder with content: " +
                result.getDriveFolder().getDriveId(),Toast.LENGTH_SHORT).show();
        Drive.DriveApi.fetchDriveId(mGoogleApiClient, String.valueOf(result.getDriveFolder().getDriveId()))
                .setResultCallback(idCallback);
    }
};

final private ResultCallback<DriveApi.DriveIdResult> idCallback = new ResultCallback<DriveApi.DriveIdResult>() {
    @Override
    public void onResult(DriveApi.DriveIdResult result) {
        if (!result.getStatus().isSuccess()) {
      //      showMessage("Cannot find DriveId. Are you authorized to view this file?");
            Toast.makeText(MainActivity.this,"Cannot find DriveId. Are you authorized to view this file?",Toast.LENGTH_SHORT).show();
            return;
        }
     DriveId   mFolderDriveId = result.getDriveId();
        Drive.DriveApi.newDriveContents(mGoogleApiClient)
                .setResultCallback(driveContentsCallback);
    }
};

final   ResultCallback<DriveApi.DriveContentsResult> driveContentsCallback =
        new ResultCallback<DriveApi.DriveContentsResult>() {
            @Override
            public void onResult(DriveApi.DriveContentsResult result) {
                if (!result.getStatus().isSuccess()) {
                    // display an error saying file can't be opened
                    return;
                }

                else{
                    CreateFileOnGoogleDrive(result);
                }
                // DriveContents object contains pointers
                // to the actual byte stream
                DriveContents contents = result.getDriveContents();
            }


        };


public void CreateFileOnGoogleDrive(DriveApi.DriveContentsResult result){

    final DriveContents driveContents = result.getDriveContents();

    // Perform I/O off the UI thread.
    new Thread() {
        @Override
        public void run() {
            // write content to DriveContents
            OutputStream outputStream = driveContents.getOutputStream();
            Writer writer = new OutputStreamWriter(outputStream);
            try {
                writer.write("Hello World");
                writer.close();

            } catch (IOException e) {
                Log.e(TAG, e.getMessage());
            }

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

            // create a file in root folder
            Drive.DriveApi.getRootFolder(mGoogleApiClient)
                    .createFile(mGoogleApiClient, changeSet, driveContents).setResultCallback(fileCallback);
        }
    }.start();
}
final private ResultCallback<DriveFolder.DriveFileResult> fileCallback = new
        ResultCallback<DriveFolder.DriveFileResult>() {
            @Override
            public void onResult(DriveFolder.DriveFileResult result) {
                if (!result.getStatus().isSuccess()) {
                    Toast.makeText(MainActivity.this,"Error while trying to create the file",Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(MainActivity.this,"Created a file with content: " +
                        result.getDriveFile().getDriveId(),Toast.LENGTH_SHORT).show();
            }
        };

我尝试过这种方法,但问题出在

final private ResultCallback<DriveApi.DriveIdResult> idCallback = new ResultCallback<DriveApi.DriveIdResult>() {
    @Override
    public void onResult(DriveApi.DriveIdResult result) {
        if (!result.getStatus().isSuccess()) {
      //      showMessage("Cannot find DriveId. Are you authorized to view this file?");
            Toast.makeText(MainActivity.this,"Cannot find DriveId. Are you authorized to view this file?",Toast.LENGTH_SHORT).show();
            return;
        }
     DriveId   mFolderDriveId = result.getDriveId();
        Drive.DriveApi.newDriveContents(mGoogleApiClient)
                .setResultCallback(driveContentsCallback);
    }
};

返回结果.getStatus.isSuccess为false。请帮我介绍如何在google驱动器中的文件夹中创建文件。

1 个答案:

答案 0 :(得分:0)

您正在致电

Drive.DriveApi.getRootFolder(mGoogleApiClient).createFile

(毫不奇怪)在根文件夹中创建了一个文件。 相反,你应该打电话给

 DriveFolder.createFile 

https://developers.google.com/android/reference/com/google/android/gms/drive/DriveFolder.html#createFile(com.google.android.gms.common.api.GoogleApiClient,com.google.android.gms.drive.MetadataChangeSet,com.google.android.gms.drive.DriveContents)

所述