Over a million developers have joined DZone.

Android Cloud Storage API Tutorial: Unified Access

Want to store your app's data in the cloud? Android's Cloud Storage API is the best way to get there.

· Mobile Zone

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 APIs depending on the cloud storage selected. Just to name a few of these providers we have Google Drive, DropBox, Box, and so on. The Android cloud storage API is a set of APIs exposed by a cloud storage providers, enabling an Android app to download and upload files as well as manage remote files or directories. This API is 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 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.

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.

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 kinds of cloud storage like Google drive and Onedrive. The mechanism is the same showed above.

The last step is implementing a file picker so that 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.

Topics:
cloud ,app ,android ,api

Published at DZone with permission of Francesco Azzola, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
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.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}