Android Code Snippet Upload files with WebView

Discussion in 'Code Snippets' started by Erel, Oct 25, 2018.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Code requires Android 5+

    1. Set a custom WebViewChromeClient. It is implemented with inline Java code.

    2. The ShowFile_Chooser event is raised when the user clicks on a "browse" button.

    3. You need to get the file URI with FileProvider and call SendResult.
    Example based on ContentChooser.
    Code:
    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
           
    Log(FileName)
           
    File.Copy(Dir, FileName, Starter.Provider.SharedFolder, "TempFile")
           jo.RunMethod(
    "SendResult"Array(Starter.Provider.GetFileUri("TempFile"), FilePathCallback))
       
    Else
           jo.RunMethod(
    "SendResult"Array(Null, FilePathCallback))
       
    End If
    End Sub
    Project is attached. Don't miss the manifest editor entries that are required for FileProvider.
     

    Attached Files:

  2. Sasuke Sama

    Sasuke Sama Active Member Licensed User

    Thanx works great but
    when i use this code the WebChromeClient1_ProgressChanged sub will not work anymore
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    It will not work with WebViewExtras as it replaces the WebChromeClient.
     
  4. Sasuke Sama

    Sasuke Sama Active Member Licensed User

    Yes i figured that out is there any other way i can use both? i only need the progress bar from webviewextras so i wonder if thers other ways to use it
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    You cannot use both. You can see how I've implemented the custom WebChromeClient and add more features to it.
     
  6. Sasuke Sama

    Sasuke Sama Active Member Licensed User

    OK thanx its googling time : D
     
  7. theunis

    theunis Member Licensed User

    hi there, when I run the example the https://b4x.com:51041/websocket_with_fileupload/index.html doesnt display - any other url is fine and displays - I just need to know if it is a setting I need to change before I try an adapt this to see if it will work for my project.

    many thanks
    T
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    Your device probably doesn't recognize the certificate.
     
  9. rosippc64a

    rosippc64a Active Member Licensed User

    Hi Erel,
    maybe file download is also possible?
     
  10. DonManfred

    DonManfred Expert Licensed User

    you should use the overrideurl event and download the file using okhttputils2.
    Search the forum. There are examples.
     
  11. rosippc64a

    rosippc64a Active Member Licensed User

    Hi DonManfred,
    that was only a rhetorical question :) I know and can use the okhttputils2, but my download link goes to a download php what needs an authentication what can made only a browser (webview e.g.) and the login data is stored in php session variables what unknown by okhttputils2.
     
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