Integrate Dropbox Chooser In Android Application

Introduction

 
Nowadays all the applications' supporting files are saved in the cloud storage. This tutorial will help you to integrate the most popular cloud storage (Dropbox) in your Android Application. The Chooser is the fastest way to get files from Dropbox into your web app. It is a small JavaScript component that enables your app to get files from Dropbox without having to worry about the complexities of implementing a file browser, authentication, or managing uploads and storage.
 

Integrating DropBox chooser in Android Application 

 
The Chooser is the fastest way to get files from Dropbox into your Android app. It's a small library that enables your app to access files from Dropbox without having to worry about the complexities of implementing a file browser, OAuth, or managing uploads and storage.
 
Please follow the steps to integrate DropBox chooser in your Android Application.
  • Provide the API, type of access and Application name,
     

  • After clicking on the create app button it will show you a page like the following.


  • You can change any of the application settings on this page. Please note down the App key; this will be needed in the Android code.
  • The Android Chooser SDK zip file contains a DropboxChooserSDK this is a library project.

Adding the chooser SDK into Android Studio


You'll need to add the DropboxChooserSDK to Android Studio and then add it as a dependency in your project.

Creating the new module

  1. Open Android Studio, go to File, New Module, then click Import Existing Project (Eclipse ADT or Gradle Project) and press Next
  2. Press the "..." button next to the Source Directory field and select the DropboxChooserSDK folder you just unzipped. Press Next.
  3. Go through the rest of the module creation flow. Click Next again, and then click Finish.
  4. You should now have a DropboxChooserSDK module in your project.

Adding the dependency

  1. Within Android Studio, switch to the "Project view"
  2. Right-click on the app module and click Open Module Settings.
  3. Select the Dependencies tab.
  4. In the lower-left corner, click the "+" (Add) button, select Module dependency and select the dropboxChooserSDKmodule.
  5. Click OK, and then click OK again. You may have to wait for Gradle to sync.
Adding the chooser SDK into ECLIPSE
 
You'll need to add the DropboxChooserSDK to Eclipse and then reference it in your project.

Creating the new project

  1. Open Eclipse, go to File, New, Other, Android, then click Android Project from existing code and press Next
  2. Press Browse next to the Root Directory field and select the DropboxChooserSDK folder you just unzipped. Press Finish.
  3. You should now have a DropboxChooserSDK project.

Referencing the project

  1. In Eclipse, right-click on your existing project and click Properties.
  2. In the menu on the left-hand side, select Android.
  3. In the lower Library section, click Add and select the DropboxChooserSDK project.
Example Application
 
The Android Chooser SDK zip also contains a ChooserExample Android app project that implements an expanded version of the code below. Open it and follow along with the rest of this guide.  
  1. static final int DBX_CHOOSER_REQUEST = 0;    
  2. private Button mChooserButton;    
  3. private DbxChooser mChooser;    
  4. @Override    
  5. protected void onCreate(Bundle savedInstanceState)    
  6. {    
  7.     super.onCreate(savedInstanceState);    
  8.     setContentView(R.layout.activity_main);    
  9.     mChooser = new DbxChooser(APP_KEY);    
  10.     mChooserButton = (Button) findViewById(R.id.chooser_button);    
  11.     mChooserButton.setOnClickListener(new OnClickListener()    
  12.     {    
  13.         @Override    
  14.         public void onClick(View v)    
  15.         {    
  16.             mChooser.forResultType(DbxChooser.ResultType.PREVIEW_LINK).launch(MainActivity.this, DBX_CHOOSER_REQUEST);    
  17.         }    
  18.     });    
  19. }    

    The Chooser coordinates with the Dropbox app to allow the user to select files without having to worry about the usual authorization flow. In order to handle the response when the user returns to your app, you'll need to add a hook to onActivityResult():, 
    1. @Override    
    2. public void onActivityResult(int requestCode, int resultCode, Intent data)    
    3. {    
    4.     if (requestCode == DBX_CHOOSER_REQUEST)    
    5.     {    
    6.         if (resultCode == Activity.RESULT_OK)    
    7.         {    
    8.             DbxChooser.Result result = new DbxChooser.Result(data);    
    9.             Log.d("main""Link to selected file: " + result.getLink()); // Handle the result     
    10.         } else     
    11.         {    
    12.             // Failed or was cancelled by the user.     
    13.         }    
    14.     } else    
    15.     {    
    16.         super.onActivityResult(requestCode, resultCode, data);    
    17.     }    
    18. }   
    
    
      Read more articles on Android


      Similar Articles