Need to see if your shared folder is taking up space on your dropbox šØāš»? Find out how to check here.
Forum Discussion
tfrysinger
7 years agoExplorer | Level 4
Cancelling a file upload or download using Java 2 API
I have implemented a file upload and download capability in my Android app using the example approaches posted on GitHub. However, I need a way to interrupt the process to support the notion of 'canc...
tfrysinger
7 years agoExplorer | Level 4
Is something like this appropriate?
DbxDownloader<FileMetadata> downloader = dbxClient.files().downloadBuilder(dropboxPath + "/" + localFile)
.start();
InputStream inputStream = downloader.getInputStream();
int count;
long bytesWritten = 0;
boolean keepGoin = true;
byte data[] = new byte[WRITE_BUFFER];
while ( keepGoin && ((count = inputStream.read(data, 0, WRITE_BUFFER)) != -1)) {
outputStream.write(data, 0, count);
bytesWritten += count;
progressListener.onProgress(bytesWritten);
if ( listener != null ) {
keepGoin = listener.cancelFileOperation();
}
}
if ( bytesWritten != size) {
// Means we were interrupted
throw new UserCancelException(InTouch.getInstance().getString(R.string.interrupted_exception_cancel_xfer));
}Greg-DB
Dropbox Community Moderator
7 years agoApologies I didn't have a code sample for that handy, but yes, what you just shared looks like what I had in mind.
- tfrysinger7 years agoExplorer | Level 4
Greg -
Once I issue .start(), do I need to do anything other than close the streams to correctly end this transaction?
- Greg-DB7 years ago
Dropbox Community Moderator
You should just use DbxDownloader.close when you're finished with the downloader (which will close the InputStream returned by getInputStream for you), in addition to closing any streams you opened yourself.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!