B4J Tutorial AsyncStreamsObject - Send and receive objects over the network

Discussion in 'B4J Tutorials' started by Erel, Nov 18, 2013.

  1. Douglas Farias

    Douglas Farias Expert Licensed User

    Ok, but AStreamO_NewObject on the b4a dont save the file, only give logs.

    1° i m sending the key file:myfilename from b4j its correct now

    2° on the b4a i m get this in AStreamO_NewObject with substring , this is ok

    Code:
    Sub astreamO_NewObject(Key As String, Value As Object)
        
    If Key.StartsWith("arquivo:"Then
        
    Log(Key.SubString(8))
                
    Dim fileName As String = Value
                
    Log(fileName)
                
    Log("Received file. File size: " & File.Size(astreamO.TempFolder, fileName))
      
    End If
       
    End Sub
    but the problem is how to write a file with this name now, the astreamO_NewObject dont save the file, i think this code is in your class on b4a.
    i m sending the correct name, i m geting this correct name on b4a, and now how to save the file with correct name?
     

    Attached Files:

  2. Douglas Farias

    Douglas Farias Expert Licensed User

    done, i have made this
    Code:
    If Key.StartsWith("arquivo:"Then
        
    Log(Key.SubString(8))
                
    Dim fileName As String = Value
                
    File.Copy(astreamO.TempFolder,fileName, fp , Key.SubString(8))
                
    Log(File.ListFiles(fp))
        
    End If
    ;) ty @Erel
     
    Erel likes this.
  3. Douglas Farias

    Douglas Farias Expert Licensed User

    @Erel
    when i use this code, with big files, i have a small bug

    Code:
    If Key.StartsWith("arquivo:"Then
                
    Dim fileName As String = Value
                
    log("aqui")
                
    File.Copy(astreamO.TempFolder,fileName, fp , Key.SubString(8))
                
    Log("done")
        
    End If
    for exemple, send from pc a video with 60mb, on the b4a it make a laggg when make this File.Copy
    i see the log "aqui" like the code. log("aqui")

    my app stop respose for 5 or 10 seconds, i have try make this


    Code:
    If Key.StartsWith("arquivo:"Then
                
    Dim fileName As String = Value
                
    log("aqui")
                
    ProgressDialogShow2("carregando..."False)
                
    DoEvents
                
    File.Copy(astreamO.TempFolder,fileName, fp , Key.SubString(8))
                
    Log("done")
        
    End If
    but dont work with doevents, my app stop response too with
    Code:
    ProgressDialogShow2("carregando..."False)
             
    DoEvents
    and i dont see the ProgressDialogShow2 only the log "aqui"

    and later my app stop work for 5 - 10 seconds i see "Done" the last log.

    now the question, why this? i m know the file is bigger 60mb, but have a way to my app dont stop work when make a file.copy?
    and how can i add a progress on this? ProgressDialogShow2("Copying...", False), later done ProgressDialogHide
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    The problem is that File.Copy is running on the main thread.

    You can use the Threading library to create a sub that will copy the file with a background thread.
     
  5. Douglas Farias

    Douglas Farias Expert Licensed User

    why use a library to make a sub? can u give a simple example code?

    callsubcopy(folders, file)

    sub copy
    file.copy
    end sub
     
  6. Erel

    Erel Administrator Staff Member Licensed User

  7. Douglas Farias

    Douglas Farias Expert Licensed User

    this dont helped my @Erel
    look i made this

    Code:
    'VIDEOS
            Else If Key.SubString(8).StartsWith("videos:"Then
            
    Dim fileName As String = Value
            
    File.MakeDir(fp, "Videos")
            pasta = 
    File.Combine(fp, "Videos")
            
    ProgressDialogShow2("carregando..."False)
            
    Private l As List
            l.Initialize
            l.AddAll(
    Array As String(astreamO.TempFolder, fileName, pasta, Key.SubString(15)))
            TR.RunOnGuiThread(
    "copiar_arquivos" , Array As Object(l))


    Sub copiar_arquivos(MapObj As Object)
    Private l As List
    l.Initialize
    l = MapObj
    File.Copy(l.Get(0), l.Get(1), l.Get(2), l.Get(3))
    ProgressDialogHide
    End Sub
    but my app stop work too with RunOnGuiThread.
    only with big sizes the app stop works, 60mb+

    EDIT: 03:06
    Code:
    'VIDEOS
            Else If Key.SubString(8).StartsWith("videos:"Then
            
    Dim fileName As String = Value
            
    File.MakeDir(fp, "Videos")
            pasta = 
    File.Combine(fp, "Videos")
            
    Private l As List
            l.Initialize
            
    ProgressDialogHide
            
    ProgressDialogShow2("Copiando..."False)
            l.AddAll(
    Array As String(astreamO.TempFolder, fileName, pasta, Key.SubString(15)))
            TR.Start(Me, 
    "copiar_arquivos" , Array As Object(l))
    this code works, with tr.start

    thx erel
     
    Last edited: Dec 19, 2014
  8. Erel

    Erel Administrator Staff Member Licensed User

    Douglas Farias likes this.
  9. YIM bunchhat

    YIM bunchhat Active Member Licensed User

    I cannot send file and receive file. How to send file from phone to pc? Ex: file .pdf in sd card.....:(. Could you write me simple code that work only send file from phone to PC? your example is complicated for me.......now I am really headache...:(
     
    Last edited: Aug 11, 2015
  10. Erel

    Erel Administrator Staff Member Licensed User

    You will need to go over the code and learn it. Sending the file is relatively simple as it just uses WriteStream to send the file.
     
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