Downloading an Object Body

The following sample code shows how to download an object body with the resumable transfer.

  • // Instantiate the target KiiObject.
    Uri objUri = Uri.parse("Set the URI of an existing KiiObject here");
    KiiObject object = KiiObject.createByUri(objUri);
    
    // Create a downloader.
    KiiDownloader downloader = null;
    try {
      // Specify the file destination.
      downloader = object.downloader(getApplicationContext(), new File(
              Environment.getExternalStorageDirectory(), "sample.mp4"));
    } catch (InvalidHolderException e) {
      // The target KiiObject has been deleted or has not been saved.
    }
    
    try {
      // Start downloading.
      downloader.transfer(new KiiRTransferCallback() {
        @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 the target KiiObject.
    Uri objUri = Uri.parse("Set the URI of an existing KiiObject here");
    KiiObject object = KiiObject.createByUri(objUri);
    
    // Create a downloader.
    KiiDownloader downloader = null;
    try {
      // Specify the file destination.
      downloader = object.downloader(getApplicationContext(), new File(
              Environment.getExternalStorageDirectory(), "sample.mp4"));
    } catch (InvalidHolderException e) {
      // The target KiiObject has been deleted or has not been saved.
    }
    
    // Start 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 KiiObject instance by calling the createByUri() method.
  • Create a KiiDownloader instance by calling the downloader() method with the file reference.
  • Start downloading by calling the transfer() method.

The onProgress() method will be called as the download progresses. The method can reach 100% on the first call if the data size is small.

If the download is interrupted for some reasons, it will be suspended and SuspendedException will be thrown. In this case, you can resume the download (For the steps to resume, see Resuming a Download).

You might need to get rutime permissions for accessing an external storage during the object body downloading. See Getting runtime permissions for the implementation details.