Google Drive Android API – upload file to existing folder

Refer the google demo hosted in Github
for getting imageUri you can had code the image url or you can use the image picker ref: https://wiki.workassis.com/android-create-a-file-picker/

Details of BaseDemoActivity you can find in the google samples in github
for getting existing folder id (0B_cMuo4-XwcAZ3IzSG1jajFlWk0) open folder from google drive in the address bar it will show like this https://drive.google.com/drive/folders/0B_cMuo4-XwcAZ3IzSG1jajFlWk0

import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveApi;
import com.google.android.gms.drive.DriveContents;
import com.google.android.gms.drive.DriveFolder;
import com.google.android.gms.drive.DriveId;
import com.google.android.gms.drive.MetadataChangeSet;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class UploadImageFromGalleryActivity extends BaseDemoActivity {

    private static final String TAG = "CreateFileActivity";

    private Uri imageUri;

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

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

    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 {
                        //getting image from the local storage
                        InputStream inputStream = getContentResolver().openInputStream(imageUri);

                        if (inputStream != null) {
                            byte[] data = new byte[1024];
                            while (inputStream.read(data) != -1) {
                                //Reading data from local storage and writing to google drive 
                                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(), "0B_cMuo4-XwcAZ3IzSG1jajFlWk0") //existing folder id = 0B_cMuo4-XwcAZ3IzSG1jajFlWk0
                            .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();

        }
    };

    final private ResultCallback<DriveFolder.DriveFileResult> fileCallback = new ResultCallback<DriveFolder.DriveFileResult>() {
        @Override
        public void onResult(DriveFolder.DriveFileResult result) {
            if (!result.getStatus().isSuccess()) {
                showMessage("Error while trying to create the file");
                return;
            }
            showMessage("Created a file with content: " + result.getDriveFile().getDriveId());
        }
    };

}

 

Author: bm on May 23, 2016
Category: Android

Your comment:

Your Name

Comment:




Last articles