THIS CONTENT DOWNLOAD SHORTLY

Objective

The main objective of writing this blog post is to guide the developers to implement the Dropbox API in android, by providing step by step guidance and easy to understand the implementation of Dropbox API.

 

What is Dropbox API?

  • Dropbox API is the Application Interface provided for android devices.
  • This API allows your mobile app to store and sync files with Dropbox.
  • This API facilitates your mobile app with its own private Dropbox.

To implement Dropbox API successfully in your application. Follow the steps mentioned below.

 

Step 1 Authentic Process

Go to Url and complete the authentication process as shown below.

Dropbox API

 

Step 2 Create Custom App

After authentication click on Create app button for creating custom app.

Create App Button

 

Step 3 Select The Dropbox API

Select the Dropbox API app as shown below.

Drop-ins-App

 

Step 4 Select Type of Data

Select the type of data needed in your app to store on Dropbox, here select Files and data stores.

Files & Database

 

Step 5 Set Privacy Statement

Set the privacy statement for your all, here select No for accessing files which are already on Dropbox.

already on Dropbox

 

Step 6 What Type of files does your app need access to?

Specify the type of files accessed by your app, here select All types of file which grants full Dropbox access. 

 

All Files Types

 

Step 7 Provide App Name

In the textbox type your application name, if that name is already registered then you cannot create with the same name. After inputting the desired name of your Dropbox app click on Create app button. 

My First Application To Show

 

Step 8 DashBoard

You will be redirected to your dashboard screen which contains all the useful parameters. You have to use that parameters in your app to access Dropbox account from your device. Here as shown in the figure below the App Key and App Secret provides unique key, which you have to pass in your application to access your Dropbox account form mobile devices.

Note that In Permission type will be Full Dropbox access. 

App Key

 

Step 9 Dropbox SDK

To implement Dropbox API in your project, you are required to download SDK for Dropbox. You are required to download dropbox-android-sdk.jar and place this file in your Libs folder in your android project as shown below. 

DropBox SDK

 

Step 10 Dropbox Implementation

 

10.1 Apply Secret Key

Implementation of Dropbox in your project. Apply DROPBOX_APP_KEY and DROPBOX_APP_SECRET as a constants and copy the values of both the constants from the created application previously shown in STEP 8.

final static public String DROPBOX_APP_KEY = " Dropbox App Key here";
final static public String DROPBOX_APP_SECRET = " Dropbox App Secret";
 
final static public AccessType ACCESS_TYPE = AccessType.DROPBOX;
 

10.2 Create Session

Create Session which allows your application to authenticate to Dropbox API.

private DropboxAPI mApi;

Add the below lines in onCreate() method.

AndroidAuthSession session = buildSession();
mApi = new DropboxAPI(session);

Method for creating session:

private AndroidAuthSession buildSession() {
    AppKeyPair appKeyPair = new AppKeyPair(Constants.DROPBOX_APP_KEY,Constants.DROPBOX_APP_SECRET);
    AndroidAuthSession session;
    String[] stored = getKeys();
    if (stored != null) {
        AccessTokenPair accessToken = new AccessTokenPair(stored[0],stored[1]);
        session = new AndroidAuthSession(appKeyPair, Constants.ACCESS_TYPE,accessToken);
    } else {
        session = new AndroidAuthSession(appKeyPair, Constants.ACCESS_TYPE);
    }
return session;
}
 

10.3 AndroidManifest.xml file

Changes in the AndroidManifest.xml file

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

Note

In the above AndroidManifest.xml file there is a line of android:scheme, which contains the key. You are required to change the key value of your application which you will found form Step 8.

 

10.4 startAuthentication

Apply the code below in onActivityResult() method to complete the authentication process.

mApi.getSession().startAuthentication(Main.this)

onResume = true;
@Override
protected void onResume() {

    AndroidAuthSession session = mApi.getSession();

    if (session.authenticationSuccessful()) {
        try {
            session.finishAuthentication();
 
            TokenPair tokens = session.getAccessTokenPair();
            storeKeys(tokens.key, tokens.secret);
            setLoggedIn(onResume);
        } catch (IllegalStateException e) {
            showToast("Couldn't authenticate with Dropbox:" + e.getLocalizedMessage());
    }
}
super.onResume();
}
 
private void storeKeys(String key, String secret) {
    SharedPreferences prefs = getSharedPreferences(Constants.ACCOUNT_PREFS_NAME, 0);
    Editor edit = prefs.edit();
    edit.putString(Constants.ACCESS_KEY_NAME, key);
    edit.putString(Constants.ACCESS_SECRET_NAME, secret);
    edit.commit();
}
 

10.5 Upload file

Code to upload file in Dropbox after authentication:

public void setLoggedIn(boolean loggedIn) {
    mLoggedIn = loggedIn;
    if (loggedIn) {
        UploadFile upload = new UploadFile(Main.this, mApi, DIR, f);
        upload.execute();
        onResume = false;
    }
}
 

10.6 What is DIR?

DIR is the name of directory of Dropbox where you can upload your files and folders of specified directory. F: f parameter in UploadFile() is a file object which you want to upload in Dropbox.

 

10.7 Get All Files and Folders from Dropbox.

To get all files and folders of Dropbox, use below code:

com.dropbox.client2.DropboxAPI.Entry dirent = mApi.metadata(DIR, 1000, null, true, null);
files = new ArrayList<com.dropbox.client2.DropboxAPI.Entry>();
dir = new ArrayList<String>();
for (com.dropbox.client2.DropboxAPI.Entry ent : dirent.contents) {
    files.add(ent);
    dir.add(new String(files.get(i++).path));
}
 

10.8 Download Files from Dropbox

Code to download files from Dropbox after authentication:

private boolean downloadDropboxFile(Entry fileSelected) {
    File dir = new File(Utils.getPath());
    if (!dir.exists())
    dir.mkdirs();
    try {
        File localFile = new File(dir + "/" + fileSelected.fileName());
            if (!localFile.exists()) {
                localFile.createNewFile();
                copy(fileSelected, localFile);

            } else {
                showFileExitsDialog(fileSelected, localFile);
            }
    } catch (IOException e) {
        e.printStackTrace();
    }
return true;
}
 
 
private void copy(final Entry fileSelected, final File localFile) {
    final ProgressDialog pd = ProgressDialog.show(DropboxDownload.this,"Downloading...", "Please wait...");
    new Thread(new Runnable() {

    @Override
    public void run() {
        BufferedInputStream br = null;
        BufferedOutputStream bw = null;
        DropboxInputStream fd;
        try {
            fd = mApi.getFileStream(fileSelected.path,
            localFile.getPath());
            br = new BufferedInputStream(fd);
            bw = new BufferedOutputStream(new FileOutputStream(localFile));

            byte[] buffer = new byte[4096];
            int read;
            while (true) {
                read = br.read(buffer);
                    if (read <= 0) {
                    break;
                    }
                bw.write(buffer, 0, read);
            }
            pd.dismiss();
        } catch (DropboxException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                    if (br != null) {
                        br.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}). start();
 
}

I hope you find this blog is very helpful while Integrating Dropbox API in Android Application. Let me know in comment if you have any question regarding Dropbox API Integration in Android.

Got an Idea of Android App Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Android App Development Company in India.

I am android developer, and do development not because to get paid or get adulation by the public, but because it is fun to program. I have been working in this field since one and half year.

face mask Belial The Demon Headgear Pocket Staff Magic