1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

Android Question Upload a file to Dropbox

Discussion in 'Android Questions' started by trueboss323, Apr 29, 2019.

  1. trueboss323

    trueboss323 Active Member Licensed 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:

    Code:
    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 Stringpath As String, filename As String, uploaded As Long, size As Long)
        
    Log($"${filename} -> ${NumberFormat(uploaded / 102400)} KB /${NumberFormat(size / 102400)} kb -> ${NumberFormat(100 * (uploaded / size), 02} - 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/"FalseFalse)
       
    ' 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:
    If I comment out dbxFiles.upload and use uploadsession, it will only say "uploadsession()" in the log, but it doesn't crash.
     
    Last edited: Apr 30, 2019
  2. DonManfred

    DonManfred Expert Licensed User

    Which version of DropboxV2 Library are you using?

    I am using v0.43 and using this command nearly daily.
    Code:
    Log($"Dropbox Upload(${path},${filename}, -> ${"/BeKa.Transport/"&filename})"$)
        dbxFiles.upload(
    path, filename, "/BeKa.Transport/" & filename,False,False)
    Code:
    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.
     
  3. trueboss323

    trueboss323 Active Member Licensed User

    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:
    Code:
    File.WriteString(path, filename, "Hello world!2")
    It will fail with an error:
    If I change the string back to the original, then it will work again.
     
    Last edited: Apr 30, 2019
  4. DonManfred

    DonManfred Expert Licensed 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
     
  5. trueboss323

    trueboss323 Active Member Licensed User

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

    Attached Files:

  6. DonManfred

    DonManfred Expert Licensed User

    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.
     
  7. DonManfred

    DonManfred Expert Licensed User

    try it with upload2.
    Upload will add a NEW file (Writemode.ADD)
    Upload2 will update an existing file too (Writemode.UPDATE)

    Code:
    dbxFiles.upload2(path, filename, "/BeKa.Transport/" & filename,False,True"1234567890")
     
    José J. Aguilar likes this.
  8. trueboss323

    trueboss323 Active Member Licensed User

    Trying to use upload2 will give the same result, it will upload the first but crash on the second. With the same error.
    Code:
    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: May 1, 2019
  9. DonManfred

    DonManfred Expert Licensed 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
     

    Attached Files:

  10. trueboss323

    trueboss323 Active Member Licensed 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: May 1, 2019
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice