Over a million developers have joined DZone.

CloudBoost Android File Upload and Download

DZone's Guide to

CloudBoost Android File Upload and Download

CloudBoost is a Database as a Service which provides an API that allows developers to store, query, search, and have real-time data on multiple clients. This is the first in a series of Android tutorials that I will be writing in order to share what I've accomplished with CloudBoost—in this tutorial, we are going to create a simple Android app for storing and fetching images.

· Cloud Zone
Free Resource

Download this eBook outlining the critical components of success for SaaS companies - and the new rules you need to play by.  Brought to you in partnership with NuoDB.

What is CloudBoost?

CloudBoost is a Database as a Service. Its primary service is a complete database which provides an API that allows developers to store, query, search, and have real-time data on multiple clients.

CloudBoost is increasingly becoming popular among developers, partly because of the inherent power that it avails to developers and partly because Facebook announced that its own mobile Backend as a Service, Parse, will be closing business in early 2017. 

With all of these reasons, it's only wise for forward-looking developers to equip themselves with the CloudBoost ecosystem.

This is the first in a series of Android tutorials that I will be writing in order to share what I've accomplished with CloudBoost. If you have areas that you would like to see me cover in this series, don't hesitate to tell me in the comments. For further reference about CloudBoost, head over to the documentation page.


In this tutorial, we are going to create a simple Android app that does 5 things:

  1. Picks an image from the phone gallery
  2. Uploads this image to CloudBoost
  3. Downloads the image from CloudBoost
  4. Saves the downloaded image to internal storage
  5. Fetches the image from internal storage and shows it in an imageview

Tools Used

  • eclipse indigo 3.7 with ADT 23.0.6
  • CloudBoost JavaSDK
  • okhttp-2.4.0
  • okhttp-ws-2.4.0
  • okio-1.4
  • socket.io client for java

All the jars listed above are available to you in the libs folder once you clone the JavaSDK. Please head over to GitHub to do so.

Create the App

Create a new android application in eclipse: File—new—android application project, call it CloudBoostImageApp.

We want a simple interface so that we focus on the core 5 items listed above, so let's take a look at our main activity screen and understand the different views used.

Image title

  1. This is just a TextView with static text declared in the activity_main.xml to act as the heading for our app.
  2. This is an empty ImageView area. When we pick our image from internal storage in item 5 of our specs, we shall set it to appear in this area.
  3. Another TextView, but this time it's not static. We set text to it according to what stage we are at. At the beginning, it indicates "no image selected", when you have selected an image from the gallery, its text changes to the URL of the selected file on the local file system. When you successfully upload the image to CloudBoost, its text changes to the URL of the image on CloudBoost. We may also use it to show any error messages.
  4. Clicking this button calls the fetchImage() method which creates an intent to open the gallery so that the user can select an image of his/her choice. After selecting, we are brought back to this screen.
  5. Clicking this button uploads the image selected to CloudBoost.
  6. Clicking this button downloads the uploaded image from CloudBoost, setting it to area #2 which contains an ImageView.


Let's take a look at the layout XML that gives rise to this screen.

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" >

        android:text="Upload and download image" />

        android:layout_weight="1" />

        android:text="no image selected" />

        android:orientation="horizontal" >

            android:text="pick image" />

            android:text="Upload image" />

        android:text="Download from cloudboost" />


It's pretty self-explanatory, so we shall look at the MainActivity.java class.


In the onCreate method, we have to initialize our CloudApp with app_id and client_key, otherwise, the SDk will tell use "app_id not found".

       protected void onCreate(Bundle savedInstanceState) {
              CloudApp.init("bengi123", "mLiJB380x9fhPRCjCGmGRg==");
              info=(TextView) findViewById(R.id.info);
              image=(ImageView) findViewById(R.id.image);


Please replace the app_id and client_key with your own from the dashboard.

Fetching Image From Gallery

Image title

This stage needs 2 methods, namely:

 public void fetchImage(View view) {
              // Create intent to Open Image applications like Gallery, Google Photos
              Intent galleryIntent = new Intent(Intent.ACTION_PICK,                       android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
              // Start the Intent
              startActivityForResult(galleryIntent, RESULT_LOAD_IMG);


This method creates an intent to select an image in the gallery and calls startActivityForResult.

Image title

When the selection is completed, onActivityResult is called, so you have to override it as well:

       protected void onActivityResult(int requestCode, int resultCode, Intent data) {
              super.onActivityResult(requestCode, resultCode, data);
              if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                           && null != data) {
                     Bitmap bitmap = getImageFromPath(data.getData());
                     Log.d("ACTIVITRESULT", "bitmap:" + bitmap);

                     imageFile = bitmapToFile(bitmap, "cloudboostimage");
                     if (imageFile != null) {
                     } else
                           Toast.makeText(this, "Failed to get image", Toast.LENGTH_SHORT)
                     // imageView.setImageBitmap(bitmap);

Uploading Image to CloudBoost

Image title

We create an async task to perform this operation in a background thread:

 class uploadTask extends AsyncTask {

               * Before starting background thread Show Progress Bar Dialog
               * */
              protected void onPreExecute() {
                     UPLOAD = true;
                     DIALOG_MESSAGE = "uploading to CloudBoost";

               * uploading file in background thread
               * */
              protected String doInBackground(String... filename) {
                     if (imageFile == null) {
                           Toast.makeText(MainActivity.this, "No image file selected",
                           return null;
                     try {
                           CloudFile file = new CloudFile(imageFile);
                           file.save(new CloudFileCallback() {

                                  public void done(CloudFile arg0, CloudException arg1)
                                                throws CloudException {
                                         if (arg0 != null) {
                                                Log.d("UPLOAD TASK", "file upload complete url="
                                                              + arg0.getFileUrl());
                                                CLOUDBOOST_IMAGE_URL = arg0.getFileUrl();
                                                runOnUiThread(new Runnable() {
                                                       public void run() {
                                         } else
                                                Log.d("UPLOAD TASK",
                                                              "File upload failed:" + arg1.getMessage());


                     } catch (Exception e) {
                           Log.e("Error: ", e.getMessage());

                     return null;

               * Updating progress bar
               * */
              protected void onProgressUpdate(String... progress) {
                     // setting progress percentage

               * After completing background task Dismiss the progress dialog
               * **/
              protected void onPostExecute(String file_url) {
                     // dismiss the dialog after the file was downloaded


Notice how we retrieve the saved image's URL and save it to a global variable with the following line CLOUDBOOST_IMAGE_URL=arg0.getFileUrl();

Image title

This URL will be used in the file download phase:

Image title

This stage is the easiest when it comes to the CloudBboost part of it. It's an everyday file download code, you just use CLOUDBOOST_IMAGE_URL.

Image title

You can download the complete source code and run here.

Learn how moving from a traditional, on-premises delivery model to a cloud-based, software-as-a-service (SaaS) strategy is a high-stakes, bet-the-company game for independent software vendors. Brought to you in partnership with NuoDB.

mobile ,cloud ,dbaas ,android ,api

Published at DZone with permission of Bengi Egima, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}