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:

tiagovsilva

Member
Licensed User
Longtime User
In debug mode, it gives this errors when opening the App
 
Upvote 0

tiagovsilva

Member
Licensed User
Longtime User
I ran the app in release mode, here's the logs:
 
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

Is this normal?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…