Search code examples
androidgoogle-drive-apigoogle-play-servicesgoogle-drive-android-api

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


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
            //





}

Solution

  • 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/