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
I managed to solve it by using the file's original name (that has extension).
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
        Log(FileName)       
        Dim f As String = GetFileInfoByIndex("_display_name", FileName)
       
        File.Copy(Dir, FileName, Starter.Provider.SharedFolder, f)
        jo.RunMethod("SendResult", Array(Starter.Provider.GetFileUri(f), FilePathCallback))
    Else
        jo.RunMethod("SendResult", Array(Null, FilePathCallback))
    End If
End Sub

Sub GetFileInfoByIndex(column As String, uri As String) As String
    Dim results As String
    Dim Cur As Cursor
    Dim Uri1 As Uri
    Dim cr As ContentResolver
    cr.Initialize("")

    'if viewing by gallery
    If uri.StartsWith("content://media/") Then
        Dim i As Int = uri.LastIndexOf("/")
        Dim id As String = uri.SubString(i + 1)
        Uri1.Parse(uri)
        Cur = cr.Query(Uri1, Null, "_id = ?", Array As String(id), Null)
        Cur.Position = 0
        If Cur.RowCount <> 0 Then
            For i = 0 To Cur.ColumnCount - 1
                If Cur.GetColumnName(i) <> Null Then
                    If Cur.GetColumnName(i) = column Then
                        results = Cur.GetString2(i)
                        Exit
                    End If
                End If
            Next
        End If
    Else
        Uri1.Parse(uri)
        Cur = cr.Query(Uri1, Null, Null, Null, Null)
        Cur.Position = 0
        If Cur.RowCount <> 0 Then
            For i = 0 To Cur.ColumnCount - 1
                If Cur.GetColumnName(i) <> Null Then
                    If Cur.GetColumnName(i) = column Then
                        results = Cur.GetString2(i)
                        Exit
                    End If
                End If
            Next
        End If
    End If
    Cur.Close
   
    Return results
End Sub

My conclusion is that for the Webview to assign the MIME type on upload, the file must have extension.
 
Last edited:
Upvote 0
Top