Uploading an Object Body

The following sample code shows how to upload an object body with the transfer-at-once method.

  • // Create a KiiObject in an application-scope bucket.
    KiiObject object = Kii.bucket("AppBucket").object();
    
    // Set key-value pairs.
    object.set("title", "MyImage");
    object.set("fileSize", 783204);
    
    try {
      // Save the KiiObject.
      object.save();
    } catch (IOException e) {
      // Handle the error.
    } catch (AppException e) {
      // Handle the error.
    }
    
    // Specify a file to upload.
    File localFile = new File(Environment.getExternalStorageDirectory(),
            "myImage.jpg");
    
    try {
      // Start uploading.
      object.uploadBody(localFile, "image/jpeg");
    } catch (IOException e) {
      // Handle the error.
    } catch (AppException e) {
      // Handle the error.
    }
  • // Create a KiiObject in an application-scope bucket.
    KiiObject object = Kii.bucket("AppBucket").object();
    
    // Set key-value pairs.
    object.set("title", "MyImage");
    object.set("fileSize", 783204);
    
    // Save the KiiObject.
    object.save(new KiiObjectCallBack() {
      @Override
      public void onSaveCompleted(int token, KiiObject object, Exception exception) {
        if (exception != null) {
          // Handle the error.
          return;
        }
        // Specify a file to upload.
        File localFile = new File(Environment.getExternalStorageDirectory(),
                "myImage.jpg");
    
        // Start uploading.
        object.uploadBody(localFile, "image/jpeg", new KiiObjectBodyCallback() {
          @Override
          public void onTransferStart(KiiObject kiiObject) {
          }
    
          @Override
          public void onTransferProgress(KiiObject object, long completedInBytes, long totalSizeinBytes) {
            float progress = (float) completedInBytes / (float) totalSizeinBytes * 100.0f;
          }
    
          @Override
          public void onTransferCompleted(KiiObject object, Exception exception) {
            if (exception != null) {
              // Handle the error.
              return;
            }
          }
        });
      }
    });

Here is what is happening in the sample code:

  • Set key-value pairs (e.g., file name, file size, and the availability of an object body) in a KiiObject as needed.
  • Call the save() method to create a new KiiObject on Kii Cloud.
  • Create a reference to the target file (myImage.jpg).
  • Start uploading by calling the uploadBody() method.

You need to create a KiiObject with the save() method prior to uploading its object body with the uploadBody() method.

The non-blocking onTransferProgress() method will be called as the upload progresses. The method can reach 100% on the first call if the data size is small.

Set the content type in a form of "type/subtype". The content type sent to Kii Cloud will be used when the object body is downloaded and when the object body is published and viewed on the browser.

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