如何使用android谷歌驱动器sdk将文件上传到谷歌驱动器文件夹 [英] How to upload a file to google drive folder using android google drive sdk

查看:146
本文介绍了如何使用android谷歌驱动器sdk将文件上传到谷歌驱动器文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的代码片段,我可以访问我已经使用同一个应用程序创建的文件夹。我正在使用'com.google.android.gms:play-services-drive:9.0.0'库并引用 github上的google驱动示例。使用示例 CreateFolderInFolderActivity.java 我可以在现有文件夹内创建文件夹。我不需要创建文件夹,而是需要在现有文件夹内创建一个文件。

Using the bellow snippet I am able to access folder which I have already created with the same app. I am using 'com.google.android.gms:play-services-drive:9.0.0' library and referring google drive sample on github. using the sample CreateFolderInFolderActivity.java I am able to create folder inside an existing folder. Instead of creating folder I need to create a file inside existing folder.

public class CreateFileInsideFolderActivity  extends BaseDemoActivity {

    private static final String TAG = "CreateFileActivity";

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

        Drive.DriveApi
                .fetchDriveId(getGoogleApiClient(), "0B_cMuo4-XwcAZ3IzSG1jajFlWk0")
                .setResultCallback(idCallback);
    }

    final 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?");
                return;
            }

            DriveId driveId = result.getDriveId();

            //showMessage("driveid" + driveId.getResourceId());

            final DriveFolder folder = driveId.asDriveFolder();

            //
            // How to upload a file to this folder
            //





}


推荐答案

感谢@pinoyyid

Thanks @pinoyyid

我从wiki.workassis中找到一个例子,我在这里分享。如果有人有更好的解决方案,请与我分享

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

    }
};

我使用默认文件选取器获取图片url

I am using the default file picker for getting Image url

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

代表文件选取器: http://wiki.workassis.com/android-create-a-file-picker/

这篇关于如何使用android谷歌驱动器sdk将文件上传到谷歌驱动器文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆