Resuming a Download

A file download can be suspended automatically (e.g., by network failure) or manually (e.g., by user interruption). The suspended download can later be resumed so as to continue the download from the point where it was interrupted.

The following sample code shows how to resume a suspended download.

  • // Instantiate a bucket.
    KiiBucket bucket = Kii.bucket("mydata");
    
    // Get the transfer manager of the bucket.
    KiiRTransferManager manager = bucket.getTransferManager();
    
    // Prepare an array for storing KiiDownloader instances.
    List<KiiDownloader> downloaders = null;
    
    try {
      // Get all KiiDownloader instances.
      downloaders = manager.listDownloadEntries(getApplicationContext());
    } catch (StateStoreAccessException e1) {
      // Failed to access the local storage.
    }
    
    // Choose the downloader to resume by a method such as the user's choice.
    // This sample code simply chooses the first one.
    KiiDownloader downloader = downloaders.get(0);
    
    // Resume downloading.
    try {
      downloader.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 download is already in progress.
    } catch (SuspendedException e) {
      // The download has been suspended because of a network error or user interruption.
    } catch (TerminatedException e) {
      // The download 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("mydata");
    
    // Get the transfer manager of the bucket.
    KiiRTransferManager manager = bucket.getTransferManager();
    
    // Prepare an array for storing KiiDownloader instances.
    List<KiiDownloader> downloaders = null;
    try {
      // Get all KiiDownloader instances.
      downloaders = manager.listDownloadEntries(getApplicationContext());
    } catch (StateStoreAccessException e1) {
      // Failed to access the local storage.
    }
    
    // Choose the downloader to resume by a method such as the user's choice.
    // This sample code simply chooses the first one.
    KiiDownloader downloader = downloaders.get(0);
    
    // Resume downloading.
    downloader.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 KiiDownloader instances by calling the listDownloadEntries() method.
  • Select the KiiDownloader instance to resume, and then call the transfer() method.

Checking the status of a KiiDownloader

The above sample code gets a list of KiiDownloader instances from the KiiRTransferManager instance. You can get the status of each KiiDownloader instance.

The progress information you can get from a KiiDownloader instance includes the bytes transferred, the bytes in total, and the status (e.g., ongoing or suspended). See Checking the status of a KiiUploader to learn how to get the information.