上传后获取文件Google Drive的URL - Android

时间:2016-10-07 15:10:04

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

在我的应用程序中,我需要拍照并管理其商店。我有两个选项,在本地内存或Google云端硬盘中保存照片,我必须跟踪SQLite数据库中的任何照片。我的应用程序工作得很好但是我有一个很大的问题:

如果我决定将照片保存在Google云端硬盘中,当我在Google云端硬盘的活动结果中按下确定(您选择文件名和驱动器文件夹)时,我必须获取该文件的网址上传到我的谷歌驱动器帐户。 我怎样才能做到这一点?我想我必须在onActivityResult方法中做点什么。

我将当前的应用程序代码保留了下来! 注意:位图newPhoto包含相机拍照;)

谢谢你的答案!

private void saveFileToDrive() {

    //Start creating new content and setting a callback
    Log.i(TAG, "Creating new Content");

    final Bitmap image = newPhoto;

    Drive.DriveApi.newDriveContents(mGoogleApiClient)
            .setResultCallback(new ResultCallback<DriveContentsResult>() {

                @Override
                public void onResult(@NonNull DriveContentsResult result) {

                    //if operation was not successful we cannot do anything
                    //and ust fail
                    if (!result.getStatus().isSuccess()) {
                        Log.i(TAG, "Fail to create new content");
                        return;
                    }

                    // otherwise, we can write our data to the new content
                    Log.i(TAG, "New content created");

                    //creation of an outputstream for the new content
                    OutputStream outputStream = result.getDriveContents().getOutputStream();
                    //Write the bitmap data from it
                    ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
                    image.compress(Bitmap.CompressFormat.PNG, 100, bitmapStream);

                    try {
                        outputStream.write(bitmapStream.toByteArray());
                    } catch (IOException e1) {
                        Log.i(TAG, "Unable to write file contents");
                    }

                    // Create the initial metadata (Type non title)
                    // NB: user is able to change the title
                    MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
                            .setMimeType("image/jpeg")
                            .setTitle(imageFileName + ".jpg").build();

                    // Create an intent for the file chooser
                    IntentSender intentSender = Drive.DriveApi
                            .newCreateFileActivityBuilder()
                            .setInitialMetadata(metadataChangeSet)
                            .setInitialDriveContents(result.getDriveContents())
                            .build(mGoogleApiClient);

                    try {
                        startIntentSenderForResult(intentSender, DRIVE_REQUEST, null, 0, 0, 0);
                    } catch (SendIntentException e) {
                        Log.i(TAG, "Fail to lauch file chooser");
                    }
                }
            });

}

After Take a Photo

Google Drive activity result

0 个答案:

没有答案
相关问题