Android Question Upload files with WebView

tiagovsilva

Member
Licensed User
Longtime User
Hi,
Im trying to upload files with WebView using this example: https://www.b4x.com/android/forum/threads/upload-files-with-webview.98623/
Upload files with WebView:
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

#if Java
import android.webkit.*;
import android.webkit.WebChromeClient.*;
import android.net.*;
public static void SendResult(Uri uri, ValueCallback<Uri[]> filePathCallback) {
    BA.Log("value = " + uri);
    if (uri != null)
        filePathCallback.onReceiveValue(new Uri[] {uri});
    else
        filePathCallback.onReceiveValue(null);
     
}
public static class MyChromeClient extends WebChromeClient {
[USER=69643]@override[/USER]
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback,
            FileChooserParams fileChooserParams) {
        processBA.raiseEventFromUI(this, "showfile_chooser", filePathCallback, fileChooserParams);
        return true;
    }
    }
#End If


I can select the file but the html input inside webview doesnt get it.
These messeges appear in the B4A Logs:
sending message to waiting queue (OnActivityResult)
running waiting messages (1)


Can anyone help me?
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

tiagovsilva

Member
Licensed User
Longtime User
In debug mode, it gives this errors when opening the App
Logger connected to: Xiaomi Mi 9 SE
--------- beginning of main
Copying updated assets files (2)
--------- beginning of crash
java.lang.RuntimeException: Unable to create service educadores.cadernetadigital.starter: java.lang.RuntimeException: java.net.SocketException: Connection reset
at android.app.ActivityThread.handleCreateService(ActivityThread.java:4023)
at android.app.ActivityThread.access$1600(ActivityThread.java:224)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1903)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7562)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Caused by: java.lang.RuntimeException: java.net.SocketException: Connection reset
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:175)
at educadores.cadernetadigital.starter.onCreate(starter.java:48)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:4011)
... 8 more
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:215)
at java.net.SocketInputStream.read(SocketInputStream.java:144)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:248)
at java.io.BufferedInputStream.read(BufferedInputStream.java:267)
at java.io.DataInputStream.readByte(DataInputStream.java:268)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:344)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
 
Upvote 0

tiagovsilva

Member
Licensed User
Longtime User
I ran the app in release mode, here's the logs:
Logger connected to: Xiaomi Mi 9 SE
--------- beginning of main
*** Service (starter) Create ***
Using FileProvider? true
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
--------- beginning of system
*** Service (firebasemessaging) Create ***
** Service (firebasemessaging) Start **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
content://com.android.providers.media.documents/document/image%3A66687
value = content://educadores.cadernetadigital.provider/name/TempFile
** Activity (main) Resume **
 
Upvote 0

tiagovsilva

Member
Licensed User
Longtime User
Yes
B4X:
AddManifestText(<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="18" />)

AddApplicationText(
  <provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="$PACKAGE$.provider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
  android:name="android.support.FILE_PROVIDER_PATHS"
  android:resource="@xml/provider_paths"/>
  </provider>)
CreateResource(xml, provider_paths,
   <files-path name="name" path="shared" />)
 
Upvote 0

tiagovsilva

Member
Licensed User
Longtime User
I tested it right now, the example works

Is there any way to print the webview logs without WebViewExtras's
addWebChromeClient function?
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
#if Java
import android.webkit.*;
import android.webkit.WebChromeClient.*;
import android.net.*;
public static void SendResult(Uri uri, ValueCallback<Uri[]> filePathCallback) {
BA.Log("value = " + uri);
if (uri != null)
filePathCallback.onReceiveValue(new Uri[] {uri});
else
filePathCallback.onReceiveValue(null);

}
public static class MyChromeClient extends WebChromeClient {
@override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback,
FileChooserParams fileChooserParams) {
processBA.raiseEventFromUI(this, "showfile_chooser", filePathCallback, fileChooserParams);
return true;
}
@override
    public boolean onConsoleMessage(android.webkit.ConsoleMessage consoleMessage) {
     
 BA.Log(consoleMessage.message());
        return true;
    }


}
#End If
 
Upvote 0

tiagovsilva

Member
Licensed User
Longtime User
I printed on the JS side the following:
JavaScript:
let file = files[0];

console.log("File Object:", file);
console.log("Name:", file.name);
console.log("Type:", file.type);
console.log('Size:', file.size);
The file object does not have the type property
File Object: [object File]
Name: TempFile
Type:
Size: 442652

Do you know how to solve this?
 
Upvote 0

tiagovsilva

Member
Licensed User
Longtime User
I dont know if this makes sense but I wanted to set the file type on Activity Resume, like this:
B4X:
Dim i As Intent
i = Activity.GetStartingIntent
i.setType(any_file_type)

On Activity Resume, after selecting the file, the intent has no extras and the intent action is MAIN
B4X:
Sub Activity_Resume
    Dim i As Intent
    i = Activity.GetStartingIntent

    Log(i.ExtrasToString())
    Log(i.Action)   
End Sub

** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
content://com.android.providers.media.documents/document/image%3A66638
value = content://educadores.cadernetadigital.provider/name/66638
** Activity (main) Resume **
no extras
android.intent.action.MAIN
Is this normal?
 
Upvote 0
Top