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: http://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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | 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()); } }; } |