Android Question Upload a file to Dropbox

trueboss323

Active Member
Licensed User
Longtime User
Hello I am using this library here to upload files to my own Dropbox acount:
https://www.b4x.com/android/forum/threads/dropbox-sdk-v2-java.74019/#content
I followed the instructions HERE and HERE
In my case, I usually want to upload small files to Dropbox so I used upload instead of uploadsession:

This is how my code looks:

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim auth As DbxAuth
    Dim Dropbox As DropboxV2
    Dim client As DbxClientV2
    Dim config As DbxRequestConfig
    Dim dbxFiles As DbxUserFilesRequests
    Dim dbxSharing As DbxUserSharingRequests
    Dim dbxUsers As DbxUserUsersRequests
    Dim token As String = "...."
    Dim DropboxEnabled As Boolean
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("1")
   
    Log("Token available. Dropbox enabled")
   
    config.Initialize("",token,"","en-us",5)
    Dim dbxhost As DbxHost
    dbxhost.Initialize
    client.Initialize("Dropbox",config,token,dbxhost)
    dbxFiles = client.files
    dbxFiles.setEventname("dbxFiles")
    dbxSharing = client.sharing
    dbxSharing.setEventname("dbxSharing")
    dbxUsers = client.users
    dbxUsers.setEventname("dbxUsers")

End Sub

Sub dbxFiles_UploadProgress(sessionId As String, path As String, filename As String, uploaded As Long, size As Long)
    Log($"${filename} -> ${NumberFormat(uploaded / 1024, 0, 0)} KB /${NumberFormat(size / 1024, 0, 0)} kb -> ${NumberFormat(100 * (uploaded / size), 0, 2) } - Session ${sessionId}"$)
End Sub

Sub dbxFiles_UploadFinished(success As Boolean, metaObj As Object, sessionId As String)
    Dim meta As FileMetadata = metaObj
    Log($"dbxFiles_UploadFinished(${success} Session ${sessionId} -> ${meta})"$)
End Sub

Sub Button1_Click
   
    Dim path As String = File.DirInternal
    Dim filename As String = "test.txt"
   
    File.WriteString(path, filename, "Hello world!")
    dbxFiles.upload(path, filename, "/Hello/", False, False)
   ' dbxFiles.uploadsession(path,filename,File.Size(path,filename),0,"","/OSM/", dbxFiles.WriteModeADD)
   
   
End Sub

When I run it and press the button , it will only say: "upload()" in the logs , then it will crash with an error:
java.lang.RuntimeException: Object should first be initialized (FileMetadata).

If I comment out dbxFiles.upload and use uploadsession, it will only say "uploadsession()" in the log, but it doesn't crash.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Which version of DropboxV2 Library are you using?

I am using v0.43 and using this command nearly daily.
B4X:
    Log($"Dropbox Upload(${path},${filename}, -> ${"/BeKa.Transport/"&filename})"$)
    dbxFiles.upload(path, filename, "/BeKa.Transport/" & filename,False,False)

B4X:
Sub dbxfiles_UploadFinished(success As Boolean, meta As FileMetadata, sessionId As String)
    Log($"dbxfiles_UploadFinished(${success}, ${meta}, ${sessionId})"$)
End Sub

Note that the Uploadsession is only needed for files > 150mb though you can use it if the file is 64kb or more too... Uploadsession raises progress events. Upload only one when finished. Can be long time for 150mb and a slow connection.
 
Upvote 0

trueboss323

Active Member
Licensed User
Longtime User
Which version of DropboxV2 Library are you using?
I have 0.44.
Trying your code has worked for me! Was I not initializing it right before?
Trying it once uploads it, then when I change the string to something like:
B4X:
File.WriteString(path, filename, "Hello world!2")

It will fail with an error:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object de.donmanfred.dbxv2.files.FileMetadataWrapper.getObject()' on a null object reference
If I change the string back to the original, then it will work again.
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
i dont understand your code!
WHEN exactly are you using this? Why THIS code should raise the error?

Upload a small project which shows the problem.
start the upload after
File.WriteString(path, filename, "Hello world!2")
with the same path and filename should work.

If that is not the issue then you need to much better describe it
 
Upvote 0

trueboss323

Active Member
Licensed User
Longtime User
i dont understand your code!
WHEN exactly are you using this? Why THIS code should raise the error?

Upload a small project which shows the problem.
start the upload after

with the same path and filename should work.

If that is not the issue then you need to much better describe it

It means if I try to upload again with a different string then it crashes.
I uploaded a project that shows the issue.
 

Attachments

  • Dropbox upload.zip
    9.4 KB · Views: 327
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
It means if I try to upload again with a different string then it crashes.
i see it too. It raises an uploadexception. The error happens in the Dropbox SDK.

Possible solutions. Delete the file if it already exists (need to do a filelist first). Or give the file another filename.
i´ll check if there is a newer jar available and if so whether it works with this.
 
Upvote 0

trueboss323

Active Member
Licensed User
Longtime User
Trying to use upload2 will give the same result, it will upload the first but crash on the second. With the same error.
B4X:
Sub Button1_Click
    Dim path As String = File.DirInternal
    Dim filename As String = "test.txt"

    File.WriteString(path, filename, "Hello world!")
 
    'Try to upload once
    Log($"Dropbox Upload(${path},${filename}, -> ${"/BeKa.Transport/"&filename})"$)
    'dbxFiles.upload(path, filename, "/BeKa.Transport/" & filename,False,False)
    dbxFiles.upload2(path, filename, "/BeKa.Transport/" & filename,False,True, "1234567890")
End Sub

Sub Button2_Click
    Dim path As String = File.DirInternal
    Dim filename As String = "test.txt"
 
    'Try upload again with an updated string
    File.WriteString(path, filename, "Hello world!2")
    'dbxFiles.upload(path, filename, "/BeKa.Transport/" & filename,False,False)
    dbxFiles.upload2(path, filename, "/BeKa.Transport/" & filename,False,True, "1234567890")

End Sub
Also , what are "mute" and "value" parameters used for in upload2?
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
for the mute you need to check Dropboxdocumentation. I can´t remember.
the value parameter is something special in the sdk. i guess you may find some infos in their documentation too.

Away from that i changed the Writemode.ADD to Writemode.OVERWRITE.
This way Upload(...) is working for me with your example.

Try the attached Version
 

Attachments

  • DropboxV2V0.46.zip
    112.7 KB · Views: 322
Upvote 0

trueboss323

Active Member
Licensed User
Longtime User
Yeah the new version is working for me , but only for upload and not upload2. Thank you Don.
An unrelated question , is there also a way to check if a file/folder exists on Dropbox?
 
Last edited:
Upvote 0
Top