SDK for Android Uploading and Downloading Files
Uploading and downloading files using the SDK for Android
Uploading a File
Files can be uploaded to Amazon Drive using uploadFile()
and uploadFileAsync()
methods. The parent nodes for this new file can be specified by using setParents()
on the UploadFileRequest.
// Create upload request.
UploadFileRequest uploadFileRequest = new UploadFileRequest(nodeName, new FileInputStream(file), file.length());
// Set the parent node IDs.
uploadFileRequest.setParents(parents);
// Perform upload (blocks until upload is complete).
UploadFileResponse response = client.uploadFile(uploadFileRequest, uploadProgressListener);
Parents
The UploadFileRequest
optionally takes parents. If no parents are set, then the new node is assigned the root as its parent.
Suppressing Deduplication
Amazon Drive rejects uploads that have the same SHA-256 as an existing file in the same drive. If it is desired to have multiple copies of the same file, then you should use the deduplicate suppression option.
// Allow duplicate binary files to be uploaded.
uploadFileRequest.setSuppress(Suppress.Deduplication);
Retrying
Uploads are not automatically retried by AmazonCloudDriveClient because the InputStream may be in a different state than the original state. To retry uploads, create a new instance of the UploadFileRequest
with a new InputStream
and call uploadFile()
again.
Downloading a File
Files can be downloaded using the downloadFile()
and downloadFileAsync()
methods.
// Create download file request.
DownloadFileRequest downloadFileRequest = new DownloadFileRequest(nodeId, new FileOutputStream(file));
// Perform download; blocks activity until download is complete.
DownloadFileResponse response = client.downloadFile(downloadFileRequest, progressListener);
Retrying
Downloads are not automatically retried by AmazonCloudDriveClient
because the OutputStream
may be in a different state than the original state. To retry uploads, create a new instance of the DownloadFileRequest
with a new OutputStream
and call downloadFile again.
Protecting Customer Content
Amazon Drives contain personal content such as photos, videos, and documents. Download files to the internal storage directory so that this content is not available to other applications. If your application requires another application to use the downloaded file, then create a ContentProvider
that will provide this data. For more details about how to share files with appropriate restrictions, see the Secure File Sharing section on the Android Developer website.
Monitoring Progress
Upload and download APIs have a special handler for monitoring progress called ProgressListener
. The progress callback method happens on the background thread. If you are updating a ProgressBar
or other UI element, then you must send an update from the onProgress()
callback method to the main thread.
// In this example, there is a handler (mHandler) that is able to post to the
// UI thread.
ProgressListener progressListener = new ProgressListener() {
@Override
public void onProgress(final long progress, final long maxProgress) {
mHandler.post(new Runnable() {
@Override
public void run() {
if (mProgressBar != null) {
// The progress from the client is a long, but Android
// progress bars require an int. This is usually not an
// issue, but can be a problem for very large files.
// The progress is rebased to Integer.MAX_VALUE to avoid
// this issue.
mProgressBar.setIndeterminate(false);
mProgressBar.setMax(Integer.MAX_VALUE);
mProgressBar.setProgress((int)(Integer.MAX_VALUE * (progress / (double) maxProgress)));
}
}
} );
}
};
Support
If you have any questions, see the Developer Forum.