How to upload a file to google drive folder using android google drive sdk

只谈情不闲聊 提交于 2019-11-29 12:01:09

Thanks @pinoyyid

I fount an example from wiki.workassis that I am sharing here. If anyone have better solution please share with me

@Override
public void onConnected(Bundle connectionHint) {
    super.onConnected(connectionHint);

    Drive.DriveApi.newDriveContents(getGoogleApiClient()).setResultCallback(driveContentsCallback);
}

In result call back

final private ResultCallback<DriveApi.DriveContentsResult> driveContentsCallback = new ResultCallback<DriveApi.DriveContentsResult>() {

    @Override
    public void onResult(DriveApi.DriveContentsResult result) {

        if (!result.getStatus().isSuccess()) {
            showMessage("Error while trying to create new file contents");
            return;
        }

        final DriveContents driveContents = result.getDriveContents();

        // Perform I/O off the UI thread.
        new Thread() {
            @Override
            public void run() {

                OutputStream outputStream = driveContents.getOutputStream();
                try {
                    InputStream inputStream = getContentResolver().openInputStream(imageUri);

                    if (inputStream != null) {
                        byte[] data = new byte[1024];
                        while (inputStream.read(data) != -1) {
                            outputStream.write(data);
                        }
                        inputStream.close();
                    }

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

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


                DriveApi.DriveIdResult exFolderResult = Drive.DriveApi
                        .fetchDriveId(getGoogleApiClient(), ExistingFolderID) 
                        .await();

                if (!exFolderResult.getStatus().isSuccess()) {
                    showMessage("Cannot find DriveId. Are you authorized to view this file?");
                    return;
                }

                DriveId driveId = exFolderResult.getDriveId();
                //showMessage("driveid" + driveId.getResourceId());
                final DriveFolder folder = driveId.asDriveFolder();


                // create a file on root folder
                folder.createFile(getGoogleApiClient(), changeSet, driveContents)
                        .setResultCallback(fileCallback);

            }
        }.start();

    }
};

I am using the default file picker for getting Image url

refer : http://wiki.workassis.com/google-drive-android-api-upload-file-to-existing-folder/

for file picker : http://wiki.workassis.com/android-create-a-file-picker/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!