First download the Dropbox sdk for android from HERE.
Now go to Apps Console from HERE. Here select the app type as "core". Write your app name in the space provided. You have two types of permission here: "app folder" & "full dropbox". Select app folder if you want to access a specific folder only from your Dropbox account or "full dropbox" if you want access of all the folder/files in your Dropbox account. Here you will get your "app key" and "app secret". Note these, as you will need this for your app.
Now create a new android project in Eclipse IDE. Once you have the app keys, we'll need to enter the following snippet in your AndroidManifest.xml in order for the Dropbox SDK to finish the authentication process. Insert the following code under the
Also make sure that your app has the internet permission by ensuring you have the following under the section of AndroidManifest.xml:
Now import all the Dropbox libraries and dependencies in the libs folder into your Eclipse project. To add the libraries to your build path, perform the following actions in Eclipse:
Right click on your project from the Package Explorer and go to Properties.
Select Java Build Path from the sidebar.
Add External JARs and add the libraries.
Now you're all set to start interacting with the Dropbox Java and Android SDKs. Continue on to the next tutorial for more on the authentication process and to get started using the API methods.
We'll need to put your app key and app secret in your code so we can use them to create a session and access your Dropbox account and the API. Replace
<application>
section, replacing APP-KEY
with your app key:1 2 3 4 5 6 7 8 9 10 11 12 | < activity android:name = "com.dropbox.client2.android.AuthActivity" android:launchMode = "singleTask" android:configChanges = "orientation|keyboard" > < intent-filter > <!-- Change this to be db- followed by your app key --> < data android:scheme = "db-INSERT-APP-KEY-HERE" /> < action android:name = "android.intent.action.VIEW" /> < category android:name = "android.intent.category.BROWSABLE" /> < category android:name = "android.intent.category.DEFAULT" /> </ intent-filter > </ activity > |
1 | < uses-permission android:name = "android.permission.INTERNET" ></ uses-permission > |
Now you're all set to start interacting with the Dropbox Java and Android SDKs. Continue on to the next tutorial for more on the authentication process and to get started using the API methods.
We'll need to put your app key and app secret in your code so we can use them to create a session and access your Dropbox account and the API. Replace
INSERT_APP_KEY_HERE
and INSERT_SECRET_HERE
with your app key and app secret in the code below.1 2 | final static private String APP_KEY = "INSERT_APP_KEY_HERE"; final static private String APP_SECRET = "INSERT_SECRET_HERE"; |
APP_FOLDER
) or whether you wanted full Dropbox access (DROPBOX
). Insert your choice here where it says INSERT_APP_ACCESS_TYPE_HERE
.
1 | final static private AccessType ACCESS_TYPE = AccessType.INSERT_APP_ACCESS_TYPE_HERE; |
The app keys are in place, now let's use them to create a new session. We'll use this session to create a new DropboxAPI object called mDBApi. You probably want to keep one of these around as an instance variable.
1 2 3 4 5 6 7 8 | // In the class declaration section: private DropboxAPI<AndroidAuthSession> mDBApi; // And later in some initialization function: AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET); AndroidAuthSession session = new AndroidAuthSession(appKeys, ACCESS_TYPE); mDBApi = new DropboxAPI<AndroidAuthSession>(session); |
The next step in the process is for the user to authorize your app through either the official Dropbox mobile app or via the Dropbox mobile web site. The user will be sent to a screen with your app icon and name and buttons to allow or deny access to their Dropbox. If authenticated, your session will have the user's access token pair to use with API calls. Here we get the session and start the authentication process. startAuthentication() is a function that redirects the user to the Dropbox mobile app or, failing to identify its install, redirects to the mobile website for user authorization via the default mobile browser.
1 2 | // MyActivity below should be your activity class name mDBApi.getSession().startAuthentication(MyActivity. this ); |
Upon authentication, users are returned to the activity from which they came. To finish authentication after the user returns to your app, you'll need to put the following code in your onResume function. Please note, it's important that the order and the timing of these calls stay the same. In fact, it might be easier to just copy this code verbatim rather than writing it from scratch.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | protected void onResume() { super .onResume(); // ... if (mDBApi.getSession().authenticationSuccessful()) { try { // MANDATORY call to complete auth. // Sets the access token on the session mDBApi.getSession().finishAuthentication(); AccessTokenPair tokens = mDBApi.getSession().getAccessTokenPair(); // Provide your own storeKeys to persist the access token pair // A typical way to store tokens is using SharedPreferences storeKeys(tokens.key, tokens.secret); } catch (IllegalStateException e) { Log.i( "DbAuthLog" , "Error authenticating" , e); } } // ... } |
Getting the folder listing:
To get the Dropbox folder listing follow the below code snippet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | try { ArrayList<String> folderName= new ArrayList<String>; Entry dropboxDir1 = mApi.metadata( "/" , 0 , null , true , null ); if (dropboxDir1.isDir) { List<Entry> contents1 = dropboxDir1.contents; if (contents1 != null ) { folderName.clear(); for ( int i = 0 ; i < contents1.size(); i++) { Entry e = contents1.get(i); String a = e.fileName(); if (String.valueOf(e.isDir).equalsIgnoreCase( "true" )){ folderName.add(a); } } } }} catch (Exception ex) { } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | try { ArrayList<String> file_name= new ArrayList<String>; Entry dropboxDir1 = mApi.metadata( "/" +folderName.get(pos), 0 , null , true , null ); if (dropboxDir1.isDir) { System.out.println( "___isdir" ); List<Entry> contents1 = dropboxDir1.contents; if (contents1 != null ) { file_name.clear(); for ( int i = 0 ; i < contents1.size(); i++) { Entry e = contents1.get(i); String a = e.path; file_name.add(a); } } }} catch (Exception ex) { } |
Download a file:
To download a file follow the below code snippet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | FileOutputStream outputStream = null ; try { sdCard = Environment.getExternalStorageDirectory(); File file = new File(sdCard.getAbsolutePath()+ "/temp" ); outputStream = new FileOutputStream(file); @SuppressWarnings ( "unused" ) DropboxFileInfo info = mApi.getFile(file_name.get( 0 ), null , outputStream, null ); } catch (Exception e) { System.out.println( "Something went wrong: " + e); } finally { if (outputStream != null ) { try { outputStream.close(); } catch (IOException e) { System.out.println( "___" +e); } } } |
Can I Store this mDBApi Permanently Because I need to use this session After my Phone reboot and I don't want to signIn again for getting the session...
ReplyDeletewhat is file_name in download code? Please answer me immediately .Thank you so much
ReplyDeletehi
ReplyDeletethe file_name is the arraylist to store the list of files..Please check above block of code.
hi
ReplyDeletethe file_name is the arraylist to store the list of files..Please check above block of code.
how to use web view for authentication within application
ReplyDelete