0

In my app, I need to upload multiple files (1 sqlite db file and multiple image files) for backup purpose to user's google drive. I am using android google drive api, but not sure, how to do back to back file uploads and then later on downloads like this.

The db file obviously comes from /data/data//databases kind of directory whereas images are stored in pictures directory. I need to grab all of these one by one and upload to drive.

Also, I have seen that if a given file (with the same title) already exists, even then, a new file with the same title is created on drive (obviously has diff DriveId but same title). I would like to check, if the file exists and only upload if it doesn't, else skip that file.

Please help.. I have been trying to refer the android demos on github by google, but have been only able to do bits and pieces using that.

omkar.ghaisas
  • 235
  • 5
  • 19

2 Answers2

2

File title is not unique in Drive API. However you can save the IDs of your newly created files in your app's local storage, so you can check against the IDs in the Drive side when you want to upload the file again.

You can use the CreateFileActvity.java from the Google Drive Demo GitHub page. It will return a file ID after you create a file successfully, so you can store the ID in your local storage.

Sample code from CreateFileActivity.java:

final private ResultCallback<DriveFileResult> fileCallback = new
            ResultCallback<DriveFileResult>() {
        @Override
        public void onResult(DriveFileResult result) {
            if (!result.getStatus().isSuccess()) {
                showMessage("Error while trying to create the file");
                return;
            }
            showMessage("Created a file with content: " + result.getDriveFile().getDriveId());
        }
    };
ztan
  • 6,861
  • 2
  • 24
  • 44
  • I am fine with using the approach given in the github sample, but there is no search result which shows me, how a repetitive file upload can be done. The problem with this approach is that the connection gets closed as soon as the file is written and a fileid is returned. I would like to upload multiple files one after the other and I haven't been able to find a way around that. – omkar.ghaisas Dec 09 '14 at 19:02
2

Just in case somebody is looking how to upload multiple files to Drive, here is solution that worked for me:

for(String fileName: fileNameArrayList){backupImage(fileName);}

 private void backupImage(String fileName) {
    Drive.DriveApi.newDriveContents(mGoogleApiClient).setResultCallback(
            new BackupImagesContentsCallback(mContext, mGoogleApiClient, fileName));
}

Backup callback:

public class BackupImagesContentsCallback implements ResultCallback<DriveApi.DriveContentsResult> {

@Override
public void onResult(@NonNull DriveApi.DriveContentsResult driveContentsResult) {
    if (!driveContentsResult.getStatus().isSuccess()) {
        Log.v(TAG, "Error while trying to backup images");
        return;
    }

    MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
            .setTitle(mFileName) // Google Drive File name
            .setMimeType("image/jpeg")
            .setStarred(true).build();

    Drive.DriveApi.getAppFolder(mGoogleApiClient)
            .createFile(mGoogleApiClient, changeSet, driveContentsResult.getDriveContents())
            .setResultCallback(backupImageFileCallback);
}

final private ResultCallback<DriveFolder.DriveFileResult> backupImageFileCallback = new ResultCallback<DriveFolder.DriveFileResult>() {
    @Override
    public void onResult(@NonNull DriveFolder.DriveFileResult result) {
        if (!result.getStatus().isSuccess()) {
            Log.v(TAG, "Error while trying to backup images");
            return;
        }
        DriveFile mImageFile;
        mImageFile = result.getDriveFile();
        mImageId = result.getDriveFile().getDriveId();
        mImageFile.open(mGoogleApiClient, DriveFile.MODE_WRITE_ONLY, (bytesDownloaded, bytesExpected) -> {
        }).setResultCallback(backupImagesContentsOpenedCallback);
    }
};

final private ResultCallback<DriveApi.DriveContentsResult> backupImagesContentsOpenedCallback =
        new ResultCallback<DriveApi.DriveContentsResult>() {
            @Override
            public void onResult(@NonNull DriveApi.DriveContentsResult result) {
                if (!result.getStatus().isSuccess()) {
                    return;
                }

                DriveContents contents = result.getDriveContents();
                BufferedOutputStream bos = new BufferedOutputStream(contents.getOutputStream());
                byte[] buffer = new byte[1024];
                int n;

                File imageDirectory = new File(mContext.getFilesDir(),
                        Constants.IMAGE_DIRECTORY_NAME);

                try {
                    FileInputStream is = new FileInputStream(new File(imageDirectory,
                            mFileName));
                    BufferedInputStream bis = new BufferedInputStream(is);

                    while ((n = bis.read(buffer)) > 0) {
                        bos.write(buffer, 0, n);
                    }
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                contents.commit(mGoogleApiClient, null);
            }
        };
}

This is not perfect solution, just a working code.

batynchuk
  • 21
  • 2