Android cloud storage API tutorial: Unified Access

Android cloud storage is a way to store data in the cloud. There are several cloud storage providers with different features and price plans. Usually, cloud storage is used to store or save files like images, documents and so on. When we want to access it from our Android app, we have to use a set of API depending on the cloud storage selected. Just to name a few of these providers we have:

  • Google drive
  • DropBox
  • Box

and so on. Android cloud storage API is a set of API exposed by a cloud storage providers. These Android cloud storage API enable an Android app to download and upload files and manage remote files or directories. These set of API are coupled with cloud providers and each provider has a specific set of features. The result is that if we want to use more that one cloud providers we have to support different API.

To overcome this problem, CloudRail developed an interesting Android library. It abstracts multiple cloud provider API providing a single unified interface.

CloudRail Unified Cloud API

CloudRail Universal Cloud API is a free Android library.  It provides a set of API to access several cloud providers. The access happens in the same way, no matter what providers we are using. In this way an Android app can support different cloud providers at the same time. The benefits are:

  • Simple and standard API to access different cloud providers API
  • Easy authentication independent from the providers we are using
  • No need to modified the Android app when the provider API changes
  • Easily switch between cloud providers in a transparent way

All these features are free. With CloudRail Universal API it is possible to perform several remote file operation. we can rename a file, upload or download a file, delete a file and so on. The same operations are performed on the remote directories. All these in the same way independent from the provider used.

Android cloud storage API

To use this library it is necessary to declare the dependency in the grandle.build

repositories {
  maven {
    url "http://maven.cloudrail.com"
  }
}

dependencies {
   compile fileTree(dir: 'libs', include: ['*.jar'])
   testCompile 'junit:junit:4.12'
   compile 'com.android.support:appcompat-v7:23.3.0'
   compile 'com.android.support:design:23.3.0'
   compile 'com.cloudrail:cloudrail-si-android:2.1.0'
}

Now we are ready to use the library. To explore this library we will develop an Android app that uploads a file. In this way, we can realize how simply is switching between different cloud providers.

Before developing the app, it is necessary to configure it using cloud provider web interface. Every provider has a different method to authorize an app, but the idea is always the same. We have to create a clientId and a secret key so that the app can access the cloud data.

If we use Dropbox, for example, we have to create an app:
Dropbox api app
dropbox api config

box secret api

Now we have the clientId and the secret key that can be used to authenticate our app. So it is time to create the CloudStorage instance. This interface provides the way to access to the remote cloud storage:

private CloudStorage getDropBoxStorage() {
   CloudStorage cs = new Dropbox(this,
                         getResources().getString(R.string.dropBoxClient),
                         getResources().getString(R.string.dropBoxSecretKey));

   return cs;
}

Done!!..Now we can access the cloud storage files and directories.

If you want to know how to access to Social Profile (like Facebook, Twtitter and so on) using a unified API give a look at Social API integration in Android
Let us suppose we selected somehow a file to upload (we will see how to do it later), to upload it to the cloud we have to use this simple piece of code

new Thread() {
   @Override
   public void run() {
     // Get asset
     InputStream is;
     try {
       File f = new File(path);
       String name = f.getName();
       is = new FileInputStream(f);
       long size = f.length();
       System.out.println("IS ["+is+"] - Size ["+size+"]");
       cs.upload("/" + name, is, size, true);
    }
    catch(IOException ioe) {
     ioe.printStackTrace();
    }
  }
}.start();

where basically, we get the InputSteam and then we call the method upload of the interface cloud storage. The file magically is uploaded to the cloud:
Dropbox upload file
As you can notice, we don’t have to know how to use specific API provided by Dropbox but the CloudRail Unified library abstracts it.
In the same way if we want to access to the Box cloud storage we have to authorize our app first:how to create app with box
In this case to create the CloudStorage, we have to write:

private CloudStorage getBoxStorage() {
   CloudStorage cs = new Box(this, 
                         getResources().getString(R.string.boxClientId),
                         getResources().getString(R.string.boxSecretKey));
   return cs;
}

Cloud Universal API supports other kind of cloud storage like Google drive and Onedrive. The mechanism is the same showed above.

Android file picker

The last step is implementing an android app with material design that has file picker. Using the file picker, we can select the file we want to upload. To this purpose, i used a simple library from android arsenal called MaterialFilePicker

We add the dependency in the build.gradle:

   ..
   compile 'com.nbsp:library:1.08'

and finally we can use the file picker. The Android app launches a file picker activity as soon as the user press the FAB:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
     Intent intent = new Intent(MainActivity.this, FilePickerActivity.class);
     intent.putExtra(FilePickerActivity.ARG_FILE_FILTER, Pattern.compile(".*\\.jpg$"));
     intent.putExtra(FilePickerActivity.ARG_DIRECTORIES_FILTER, false);
     intent.putExtra(FilePickerActivity.ARG_SHOW_HIDDEN, true);
     startActivityForResult(intent, 1);
   }
});

and when the Actvity returns we upoad the file:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == 1 && resultCode == RESULT_OK) {
       String filePath = data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH);
       Toast.makeText(this, "File path ["+filePath+"]", Toast.LENGTH_LONG).show();
       uploadFile(filePath);
    }
}

The final result of the app is shown below:
android cloud storage api
android file picker
android upload file

When the user touches on the filename, the app starts uploading the file to the cloud. As you can notice it is very simple to change the Android cloud storage provider. The interesting aspect is that is possible to perform other file operations in a transparent way. At the end of this post, you know how to use Android cloud storage API and how to manage remote files and directories.

 Source code available @ github