Resuming an Upload
A file upload can be suspended automatically (e.g., by network failure) or manually (e.g., by user interruption). The suspended upload can later be resumed so as to continue the upload from the point where it was interrupted.
The following sample code shows how to resume a suspended upload.
// Instantiate a bucket.
KiiBucket bucket = Kii . bucket ( "AppBucket" );
// Get the transfer manager of the bucket.
KiiRTransferManager manager = bucket . getTransferManager ();
// Prepare an array for storing KiiUploader instances.
List < KiiUploader > uploaders = null ;
try {
// Get all KiiUploader instances.
uploaders = manager . listUploadEntries ( getApplicationContext ());
} catch ( StateStoreAccessException e1 ) {
// Failed to access the local storage.
}
// Choose the uploader to resume by a method such as the user's choice.
// This sample code simply chooses the first one.
KiiUploader uploader = uploaders . get ( 0 );
try {
// Resume uploading.
uploader . transfer ( new KiiRTransferProgressCallback () {
@Override
public void onProgress ( KiiRTransfer operator , long completedInBytes , long totalSizeinBytes ) {
float progress = ( float ) completedInBytes / ( float ) totalSizeinBytes * 100.0f ;
}
});
} catch ( AlreadyStartedException e ) {
// The upload is already in progress.
} catch ( SuspendedException e ) {
// The upload has been suspended because of a network error or user interruption.
} catch ( TerminatedException e ) {
// The upload has been terminated because of the missing file or user interruption.
} catch ( StateStoreAccessException e ) {
// Failed to access the local storage.
}
// Instantiate a bucket.
KiiBucket bucket = Kii . bucket ( "AppBucket" );
// Get the transfer manager of the bucket.
KiiRTransferManager manager = bucket . getTransferManager ();
// Get all KiiUploader instances.
manager . listUploadEntries ( getApplicationContext (), new KiiRTransferManagerCallback () {
@Override
public void listUploadEntriesCompleted ( KiiRTransferManager manager , List < KiiUploader > uploaders , Exception e ) {
if ( e != null ) {
// Handle the error.
return ;
}
// Choose the uploader to resume by a method such as the user's choice.
// This sample code simply chooses the first one.
KiiUploader uploader = uploaders . get ( 0 );
// Resume uploading.
uploader . transferAsync ( new KiiRTransferCallback () {
@Override
public void onStart ( KiiRTransfer operator ) {
}
@Override
public void onProgress ( KiiRTransfer operator , long completedInBytes , long totalSizeinBytes ) {
float progress = ( float ) completedInBytes / ( float ) totalSizeinBytes * 100.0f ;
}
@Override
public void onTransferCompleted ( KiiRTransfer operator , Exception exception ) {
if ( exception != null ) {
// Handle the error.
return ;
}
}
});
}
});
Here is what is happening in the sample code:
Create a KiiBucket
instance of the bucket that has the target KiiObject.
Create a KiiRTransferManager
instance by calling the getTransferManager()
method.
Get a list of KiiUploader
instances by calling the listUploadEntries()
method.
Select the KiiUploader
instance to resume, and then call the transfer()
method.
Checking the status of a KiiUploader
The above sample code gets a list of KiiUploader
instances from the KiiRTransferManager
instance. You can get the status of each KiiUploader
instance.
The progress information you can get from a KiiUploader
instance includes the bytes transferred, the bytes in total, and the status (e.g., ongoing or suspended). The next sample code illustrates how to get the information.
// Get the uploader by the method shown in the above sample code.
KiiUploader uploader = uploaders . get ( 0 );
// Prepare a variable for storing the progress information.
KiiRTransferInfo info = null ;
try {
// Get the progress information of the uploader.
info = uploader . info ();
} catch ( StateStoreAccessException e ) {
// Handle the error.
return ;
}
long completedSize = info . getCompletedSizeInBytes ();
long totalSize = info . getTotalSizeInBytes ();
KiiRTransferStatus status = info . getStatus ();
// Get the uploader by the method shown in the above sample code.
KiiUploader uploader = uploaders . get ( 0 );
// Get the progress information of the uploader.
uploader . infoAsync ( new KiiRTransferCallback () {
@Override
public void onInfoCompleted ( KiiRTransfer operator , KiiRTransferInfo info , Exception e ) {
if ( e != null ) {
// Handle the error.
return ;
}
long completedSize = info . getCompletedSizeInBytes ();
long totalSize = info . getTotalSizeInBytes ();
KiiRTransferStatus status = info . getStatus ();
}
});