How to upload a file to the Google drives folder using the Android sdk

Using the following snippet, I can access the folder that I already created with the same application. I use the com.google.android.gms library: play-services-drive: 9.0.0 and link to a sample google drive on github . using the sample CreateFolderInFolderActivity.java I can create a folder inside an existing folder. Instead of creating a folder, I need to create a file inside an 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
            //





}
+1
source share
1 answer

Thanks @pinoyyid

wiki.workassis, . - , , .

@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 {
                    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-

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

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

+2

Source: https://habr.com/ru/post/1538959/


All Articles