Android Code Snippet Upload files with WebView

JohnC

Expert
Licensed User
Longtime User
OK, I modified a sub in Erel's example so it will upload the selected file using it's actual name:

B4X:
Sub ShowFile_Chooser (FilePathCallback As Object, FileChooserParams As Object)
    cc.Initialize("CC")
    cc.Show("*/*", "Choose File")
    Wait For CC_Result (Success As Boolean, Dir As String, FileName As String)
    Dim jo As JavaObject = Me
    If Success Then
        Dim FN As String = GetFileNameFromContentResult(FileName)
        Log(FileName)
        Log(FN)
        File.Copy(Dir, FileName, Starter.Provider.SharedFolder, FN)
        jo.RunMethod("SendResult", Array(Starter.Provider.GetFileUri(FN), FilePathCallback))
    Else
        jo.RunMethod("SendResult", Array(Null, FilePathCallback))
    End If
End Sub

And I added this routine:

B4X:
Sub GetFileNameFromContentResult(UriString As String) As String
    If UriString.StartsWith("/") Then 'If the user used a file manager to choose file
        Return UriString.SubString2(UriString.LastIndexOf("/")+3,UriString.Length)
    Else if UriString.IndexOf("%3A") > -1 Then
        Return UriString.SubString2(UriString.LastIndexOf("%3A")+3,UriString.Length)
    Else
        Return UriString
    End If
End Sub
 

youjunjer

Member
Licensed User
Longtime User
Use these codes, I only got a serial number, not the real file name.

 

nikitaganco

Member
Licensed User
hello, is it possible to add option to take picture from camera and upload it? on iphone it is possible and very handy.
 

JohnC

Expert
Licensed User
Longtime User
You can easily combine this code with the camera code to do that:

 

nicieri

Active Member
Licensed User
Longtime User

How can I set the main in B4X??... because I'm get an error

\b4xmainpage.java:825: error: cannot find symbol
processBA.raiseEventFromUI(this, "showfile_chooser", filePathCallback, fileChooserParams);
^
symbol: variable processBA
location: class MyChromeClient
 

Ivica Golubovic

Active Member
Licensed User
I know this is an old post, but if anyone still has issues with this topic, try:

 

rachman

Member
Licensed User
Longtime User
Yes, but I have old project program still using activities , could you give me a sample, because I have tried but still get error
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…