Life of Apps

App Integration with Dropbox

Storing user data on a location other than the device has become a mandatory for a few years now. There are many options to do so and one of the quick and easy ones is using Dropbox. I integrated my app with Dropbox to allow users to store their Preferences (Shared Preferences). This would help them to continue using the app easily when they switch to a different device or reset the current one. While Dropbox provides fairly detailed instructions to do the integration, I would like to share a summarize the steps and add a few points to keep in mind:

The steps to integrate with Dropbox are:


  1. Register for an app with Dropbox at the app console to get an app key and secret 
  2. Download the Dropbox Android SDK. A zip containing the jar file is provided. The jar has to be added to the project in the Android Studio. It is a bit disappointing that there is no Gradle option available yet.
  3. Copy paste the following code in the AndroidManifest.xml:

  4. <activity
    android:name="com.dropbox.client2.android.AuthActivity"
    android:launchMode="singleTask"
    android:theme="@android:style/Theme.Translucent.NoTitleBar"
    android:configChanges="orientation|keyboard">
    <intent-filter>
    <!-- Change this to be db- followed by your app key -->
    <data android:scheme="db-INSERT_APP_KEY" />
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.BROWSABLE"/>
    <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    </activity>

    This will either open the Dropbox app (if you have it installed on your app) or open the Dropbox page on Chrome/browser for authentication.

  5. In the code, you need to perform authentication which will invoke the activity listed above. Then you can use the Dropbox API to either put or get files from the user's Dropbox. A folder with the app name is created once the user authenticates the app in step 3. The code snippets are provided here.

Points to Note
  • Dropbox user authentication and app authorization call can be on the main thread. The onResume method of the activity becomes very important here as the control returns to that method once the authorization process is complete. So, any processing post authentication should start from onResume.
  • Reading and writing files to Dropbox are network activities and Android will throw an exception when you do this on the main thread. You need to use an AsyncTask to do these tasks. I have used two AsyncTasks to do read and write to Dropbox respectively. Both these are called from the onResume method. If called from elsewhere, a DropboxUnlinkedException will be thrown.
  • If the Dropbox API's putFile method is used to add files to the user's Dropbox, it will create multiple files with the same name, such as Prefs1, Prefs2 etc. If you would rather have only one file, then putFileOverwriteRequest should be used.
The post here summarizes the points from the Dropbox Android tutorial and a few other points that I found during the implementation. Following the steps here should help you integrate with Dropbox without much difficulty.

Danesh

Visit Pleb.in for apps developed by Danesh

No comments :

Post a Comment

Leave a Comment...