Dropbox Sync Library

Erel

B4X founder
Staff member
Licensed User
Longtime User
Dropbox has deprecated this service. Don't use in new projects!

This library wraps Dropbox Sync API. Using this API it is quite simple to store (and retrieve) data in a folder inside the user's Dropbox account.

For example if your program creates reports, you can upload the saved reports to the user's Dropbox account. The user will then be able to access the reports from any Dropbox client.

Dropbox library is responsible for the communication and the synchronization.

Using this library

1. Download the Android SDK: https://www.dropbox.com/developers/sync
Unzip it and copy dropbox-sync-sdk-android.jar to the libraries folder.
2. Copy the attached wrapper files to the libraries folder.
3. Log-in to your Dropbox account and go to the developer console: https://www.dropbox.com/developers/apps
Create a new app and make sure to choose Sync API.
The app key and app secret values are later required:

SS-2013-06-10_16.36.06.png


Basic4android code

The first step it to link an account. If this is the first time that the account is linked then the user will be asked to approve the connection.
The AccountReady event is raised after the account is linked and is ready.

You can now upload files, download files or list the existing files.
The download operation raises the DownloadCompleted event. Other operations do not raise events.
Note that the root path (/) points to the app folder.
Folders will be created automatically.
For example if you upload a file to /some/folder then the folders will be created if needed.

B4X:
Sub Process_Globals
   Private manager As DbxAccountManager
   Private key As String = "aaa"
   Private secret As String = "bbb"

End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      manager.Initialize(key, secret, "manager")
   End If
   Activity.LoadLayout("1")
End Sub

Sub Button1_Click
   manager.LinkAccount
End Sub

Sub Manager_AccountReady (Success As Boolean)
   Log("Account Ready: " & Success)
   If Success Then
      For Each FileInfo As DbxFileInfo In manager.ListFiles("/")
         Log(FileInfo.Name & ", " & FileInfo.IsFolder)
      Next
   End If
   manager.DownloadFile("/", "somefile", File.DirRootExternal, "somefile")
End Sub

Sub Manager_DownloadCompleted (Success As Boolean, LocalDir As String, LocalFileName As String)
   Log("DownloadCompleted: " & Success)
End Sub

Manifest editor code

B4X:
SetApplicationAttribute(android:label, "@string/app_name") ' NEW in v3.0!!!
AddApplicationText(
<activity android:name="com.dropbox.sync.android.DbxAuthActivity" />
<activity
  android:name="com.dropbox.client2.android.AuthActivity"
  android:launchMode="singleTask" >
  <intent-filter>
    <data android:scheme="db-YOURAPPKEY" /> <!-- NEED TO UPDATE -->
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.BROWSABLE" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>
<service
  android:name="com.dropbox.sync.android.DbxSyncService"
  android:enabled="true"
  android:exported="false"
  android:label="Dropbox Sync" />
  )

Make sure to replace YOURAPPKEY with your app key...


Resource File (new in V3.0)

Due to a bug/issue in Dropbox SDK we must set the app name with an XML file.
Create a file named strings.xml and put it under Objects\res\values.
The file content should be:
B4X:
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="app_name">B4A Example</string>
</resources>
Change B4A Example to the correct app name. Set the file to be read-only.

Edit:
V3.0 - Works with Dropbox SDK v3.0 v3.1.
V1.25 - Adds an AutoSync field. Setting this field to true will cause the cache to be synchronized before files are downloaded. The latest files will be downloaded.
V1.21 - fixes an issue with Sync event.
v1.20 released. Works with DropBox SDK v2.0

The library is available here: www.b4x.com/android/files/DropboxSync.zip
 
Last edited:

GMan

Well-Known Member
Licensed User
Longtime User
Very nice feature, missed that :sign0087:

This line causes an error:
B4X:
  manager.Initialize(key, value, "manager")

Value is not assigned before using...
 
Last edited:

GMan

Well-Known Member
Licensed User
Longtime User
Thx, so far so good - but after test compiling (without errors) an error occurs while reading the "dropbox-sync-sdk-android.jar": error in opening zip file if i try to compile to the device

SOLVED - yesterday i download only the jar-file and put it in my Lib-folder.
After the problems this morning i downloaded the whole SDK and copied the .jar-lib again to my Lib-folder.

When asking if it should be overwritten i saw, that the file from yesterday has only ~24 kb, the one from today is about 161 KB and works fine....ver special thing, because the download of the jar-lib only yesterday is the same file from the today downloaded archive.

So, DropBox-Sync works fine, Upload and downlad, connecting etc etc. :cool:

Again thx to Erel :icon_clap:
 
Last edited:

Theera

Well-Known Member
Licensed User
Longtime User
Hi Erel,
How to have 1.bal file? I couldn't seek it.
 

rboeck

Well-Known Member
Licensed User
Longtime User
Authentification problems

Hi,
in your description everything looks easy, but i miss something:
in the dropbox app console i have to name my app; is this name free to choose or is this the app Name or the app label?
Then i have to write two url's:
OAuth 2 redirect URLs
How to i get them?
And last question: How to i know, if my account settings are working?
Greetings
Reinhard
 
Last edited:

GMan

Well-Known Member
Licensed User
Longtime User
Create a small test program as described above. Try to upload a file.
You can then open your dropbox account and see the file.

If you have the DropBox-Tool also installed on your PC the task-icon will give you a popup-message that a file was uploaded/updated.
 

rboeck

Well-Known Member
Licensed User
Longtime User
By the first run, when i came to the line

B4X:
manager.Initialize(dpAppKey, dpAppSecret, "manager")

i get in then log :

java.lang.IllegalStateException: URI scheme in your app's manifest is not set up correctly. You should have a com.dropbox.client2.android.AuthActivity with the scheme: db-xxxxxxxxx <- my Appkey

I have copied the above text in the manifest file as addition and also pasted the appKey in the manifest.
Any ideas what went wrong?

Greetings
Reinhard
 

GMan

Well-Known Member
Licensed User
Longtime User
By the first run, when i came to the line

B4X:
manager.Initialize(dpAppKey, dpAppSecret, "manager")

i get in then log :

java.lang.IllegalStateException: URI scheme in your app's manifest is not set up correctly. You should have a com.dropbox.client2.android.AuthActivity with the scheme: db-xxxxxxxxx <- my Appkey


Greetings
Reinhard

Did you replace the XXXXXXXXX with your code correctly ?
 

rboeck

Well-Known Member
Licensed User
Longtime User
Hi,

it was the 'db-' at the appkey in the manifest; i pasted my key, but only the key without db-...
Now my app is running an i have to check the new possibilities...
Greetings and many thanks
Reinhard
 

GMan

Well-Known Member
Licensed User
Longtime User
:wav:
 

hypergreatthing

Member
Licensed User
Longtime User
Erel,
Thank you very much for this library. This solves an issue i'm trying to work around. However i have the following questions:
What exactly is the purpose of DbxAccountManager.Sync?
Along those same lines, i see a method for downloading and uploading. There should be a way to manually loop through the listfiles and do a islatest on them, but isn't the whole purpose of syncing more along the lines of setting a folder in your app directory as a synced folder, then everything you put in there will be pushed and synced to the dropbox remote folder?
Looking at the dropbox api, there's references to DbxFileSystem.PathListener that you can setup so that it will auto update a path/folder.

I'm planning on diving into this very shortly. I just wanted to check if
a) I'm doing it wrong thinking that i should be manually pushing and checking for updates.
b) there's no planned updates for this library to include pathlisteners in the near future.

Thanks,
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Normally you do not need to call Sync. The folder is synced automatically.
You can call Sync to force the folder to sync now.

Note that when you call Download you will always get the latest version.
I'm doing it wrong thinking that i should be manually pushing and checking for updates.
You do not need to do anything manually.

there's no planned updates for this library to include pathlisteners in the near future.
Maybe in the future. For now I don't think that they are needed as you always get the latest version when you call Download.
 

dealsmonkey

Active Member
Licensed User
Longtime User
Thanks for this libary Erel. I needed an easy way to get a file from the device to a PC and this does the job perfectly :)
 

hypergreatthing

Member
Licensed User
Longtime User
Normally you do not need to call Sync. The folder is synced automatically.
You can call Sync to force the folder to sync now.

Note that when you call Download you will always get the latest version.

You do not need to do anything manually.
Question then: In your example you download a file from dropbox. There is no folder option, or is there? I tried to download a file from dropbox, then modified the one on dropbox (in the App Folder, under the [user created program] folder). I replaced a 5 meg file with one that was like 16k. The dropbox web interface showed it was replaced. I tried to add another button in your example that called manager.sync but nothing happened to the local file that was downloaded.

I'll try out calling a manager.download without a specified filename and see if it writes the dropbox directory instead and keeps it synced.

I don't think that worked. Passing a Null value for the file produces a file called "NULL".

Am i missing something here like, the requirement to have the dropbox app installed to auto-sync downloaded or uploaded files?
 

hypergreatthing

Member
Licensed User
Longtime User
Ok. That's what i thought. And that works well.

I'm used to the dropbox on pc app where if a phone uploads a picture for instance, it will automatically detect a new file and auto download to your computer.

When i said manually before, i meant if i want a file to be pushed to dropbox, i have to execute the manager.uploadfile function. There's no way to establish a auto-sync folder where any changes inside that folder on the device locally or on dropbox will fire off a sync between the two.

Thank you.
 
Top