Dropbox SDK for Android

Last night I had a great idea for an app, which should be pretty easy to implement, I thought. I needed to use the Dropbox API for that and so I went to their website and found out that they have an own Android SDK. I could not find any instructions on how to use it and so I tried to find out by myself and downloaded that package. Next thing I tried was importing it to Eclipse, and that’s where the trouble took its course…

What went wrong?

First thing I noticed: The package didn’t contain any Eclipse project file. So I created a new Android Project in Eclipse and imported their source code – Build Error. I found out that I had to add the .jar files included in the package to my project’s Java build path. Bingo! It compiled and started the Demo application.

Cool! I could sign in and out. Now I just had to reference it as an Android Library and use it in my own project, right? The answer is no. Despite the .jar for the Dropbox Java API implementation, the “SDK” includes only two files: DropboxSample.java (the Activity) and LoginAsyncTask.java
. The second one handles all the login stuff and would be very handy when writing an app which should be connected to Dropbox, but it is so tightly coupled to the sample Activity, that it’s impossible to reuse it anywhere else. That made me pretty angry and so I decided to refactor the SDK.

Refactoring an SDK

I have used a couple of SDKs before and what I wanted to have was this:

  • An isolated Android Library Project containing only SDK classes
  • A project containing the sample application, which references the SDK project
  • A decoupled LoginAsyncTask.java
  • A small set of helper classes providing the easiest way to use the SDK

Long story short – The result is a Github project containing two projects: One SDK project, which is an Android Library, and a Sample Application. As I said before, I decoupled the LoginAsyncTask.java and made the SDK easier to use. I added a Dropbox.java class, which encapsulates the login procedure, and a LoginListener class as a simple callback. Here are some code snippets on how to use it in your Activity:

Creating the Dropbox object and checking existing logins:

final static private String CONSUMER_KEY = "PUT_YOUR_CONSUMER_KEY_HERE";
final static private String CONSUMER_SECRET = "PUT_YOUR_CONSUMER_SECRET_HERE";

// ... //

mDropbox = new Dropbox(getBaseContext(), CONSUMER_KEY, CONSUMER_SECRET);
if (mDropbox.authenticate()) {
        	// We can query the account info already, since we have stored 
        	// credentials
        	getAccountInfo();
} 

Handling the login callback:

    // Notifies our Activity when a login process succeeded or failed.     
    DropboxLoginListener mLoginListener = new DropboxLoginListener() {

		@Override
		public void loginFailed(String message) {			
			showToast("Login failed: "+message);
			setLoggedIn(false);
		}

		@Override
		public void loginSuccessfull() {
			showToast("Logged in!");
			setLoggedIn(true);		
			displayAccountInfo(mDropbox.accountInfo());
		}    	
    };

Logging in:

 private void getAccountInfo() {
    	
    	if (mDropbox.isAuthenticated()) {
		// Access credentials are already stored, just login
		mDropbox.login(mLoginListener);	            		
    	} else {
    		// Code for getting email and password goes here //

	        mDropbox.login(mLoginListener, email, password);	        
    	}
    }

For more details, just have a look at the Sample Application! If you have any feedback, feel free to comment.

Thanks so far

Advertisements

5 thoughts on “Dropbox SDK for Android

  1. Google Translator:

    Thank you for providing excellent friend with a good refactoring sdk dropbox.

    I have to work with an application using it dropbox (integrated) and I think this will help em a lot.

    I hope you keep well, if you can have a putfile () and send a file to the dropbox much, would be excellent.

    Original:

    Excelente amigo Gracias por aportar con una buena Refactorización del sdk de dropbox.

    tengo que trabajar con una aplicación que utilice dropbox en ella (integrado) y creo que esto em ayudara mucho.

    espero que sigas así, haber si puedes hacer un putFile() y enviar una a archivo a la cuanta dropbox, seria excelente.

    gracias, muchas gracias.

  2. Google Translator:

    implement a putfile is simple:

    mEmviar.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {

    File file = new File(“/sdcard/adadad.doc”);
    mDropbox.putFile(“dropbox”, “/”, file);
    }
    });

    Thank you so much friend, I hope we move to q APIs dropbox.
    Thanks, Thanks.

    Original:

    implementar un putFile es simple:

    ……………………..

    Muchisimas gracias amigo, espero q avancemos con el api de dropbox.
    Gracias, Gracias.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s