Android Question Allow file downloading from web page in Webview

Hi all, I have a simple app displaying a website through Webview control. However I noticed that file upload and download are not permitted - nothing happens when the site return a file or when a file upload dialog is requested.
How to add these functionalities? Thanks in advance

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private mainW As WebView
    Dim wextr As WebViewExtras
    Dim wsett As WebViewSettings
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    toggleFormLogout
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub toggleFormLogout
    mainW.Initialize("mainW")
    wsett.setAllowFileAccess(mainW,True)
    wextr.addJavascriptInterface(mainW,"jsEv")
    wextr.addWebChromeClient(mainW,"chromeEv")
    Activity.AddView(mainW, 0, 0, 100%x, 100%y)
    mainW.LoadUrl("https://mysite.com")
End Sub
 

max123

Well-Known Member
Licensed User
Longtime User
Hi @johnnyontheweb I've the same problem, I've a download link but nothing happen when I click on it.

You solved it? Is a permisson issue?

My code is very similar to your, I've used WebViewExtras.

Many thanks
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
you need to explain things more clearly.
#1 webview is not a browser; some things that work in a browser don't work in a webview.

you say:"file upload and download are not permitted". who says they are not permitted?
exactly what are you talking about?

you say: "nothing happens when the site return a file or when a file upload dialog is requested".
how - exactly - are you trying to carry out these operations? the code you posted shows very
little.

there are some examples here in the forum regarding file uploading. see attached.
 

Attachments

  • upload.png
    upload.png
    236.1 KB · Views: 242
Upvote 0
Hello, thanks for your reply, I read almost all the examples in this forum, but nothing helped.
I supposed this is a matter of permissions, that's why I said "not permitted", however it was part of the question... - has the permission to write the downloaded file on internal memory to be set?

I tried also the Url Override, but it was never hit:

B4X:
Private Sub mainW_OverrideUrl (Url As String) As Boolean
    If Url.EndsWith(".ext") Then
        DownloadFile(Url)
        Return True
    Else
        Return False
    End If
End Sub

' using lib OkHttpUtils2
Sub DownloadFile (link As String)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(link)
    Wait For (j) JobDone(j As HttpJob)
    j.Release
End Sub

I think I'm missing something basic
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Many thanks for reply,

needed a solution that work on Android SDK 29-30 but even old versions, so it can work at least from Android 4+ or 6+.

I'm here now to search a solution to this.

Do you tried to add android.permisson.WRITE_EXTERNAL_STORAGE to the manifest ?
 
Last edited:
Upvote 0
Yes, I used both:
B4X:
AddPermission (android.permission.READ_EXTERNAL_STORAGE)
AddPermission (android.permission.WRITE_EXTERNAL_STORAGE)

but nothing changes, mainW_OverrideUrl is not hit.
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
At this point try to use UltimateWebView from @Ivica Golubovic, I see it has Download callback, it allow a lots of webview functionalities and add permissons (some you have to add manually to the manifest) or just try to ask to author, it is expert on webview.

I cannot use UltimateWebview because my code is inside a class and I found some problems on this so I replaced with WebViewExtras.

If you try a solution please post it here, I tried for 2 days various solutions (like you) all without success.
 
Last edited:
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Upvote 0

hung

Active Member
Licensed User
Longtime User
Try this. I have an app showing Browse to choose a file to upload. the inline java code helps. I refer to some sample code in this forum but forgot where I got that.


B4X:
Sub ShowFile_Chooser (FilePathCallback As Object, FileChooserParams As Object)
    cChooser.Initialize("CC")
    cChooser.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.FProvider.SharedFolder, "TempFile")
        jo.RunMethod("SendResult", Array(Starter.FProvider.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) {
    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;
    }
    }
#End If

Private cChooser As ContentChooser
 
Upvote 0

max123

Well-Known Member
Licensed User
Longtime User
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
Hello, thanks for your reply, I read almost all the examples in this forum, but nothing helped.
I supposed this is a matter of permissions, that's why I said "not permitted", however it was part of the question... - has the permission to write the downloaded file on internal memory to be set?

I tried also the Url Override, but it was never hit:

B4X:
Private Sub mainW_OverrideUrl (Url As String) As Boolean
    If Url.EndsWith(".ext") Then
        DownloadFile(Url)
        Return True
    Else
        Return False
    End If
End Sub

' using lib OkHttpUtils2
Sub DownloadFile (link As String)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(link)
    Wait For (j) JobDone(j As HttpJob)
    j.Release
End Sub

I think I'm missing something basic

you don't need permission to write to file.dirinternal or rp.getsafedirdefaultexternal("").

if your code for the DownloadFile sub is complete, it does nothing with the downloaded
resource (if any). i say "if any" because downloading resources from a web page can
sometimes give unexpected results.

it's unclear that you are using the _OverrideUrl method correctly. @Erel has what he calls the
"smell test" for code. the code in that method makes me wonder what you're doing.
 
Upvote 0

RayGrey033

New Member
you don't need permission to write to file.dirinternal or rp.getsafedirdefaultexternal("").

if your code for the DownloadFile sub is complete, it does nothing with the downloaded
resource (if any). i say "if any" because downloading resources from a web page can
sometimes give unexpected results.

it's unclear that you are using the _OverrideUrl method correctly. @Erel has what he calls the
"smell test" for code. the code in that method makes me wonder what you're doing.
I think same
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
First, as stated before, you give very little info...
Secondly, also as stated before, a web view is not a web browser. Some features are not available in a webview but are available in a web browser.

You should use the web view to SHOW your desired page and use httputils to up/download files.
 
Upvote 0
Thanks for all replies.
Good to know that no premissions is required to save downloaded files.

You should use the web view to SHOW your desired page and use httputils to up/download files.
this is exaclty what I'm try to do, see my last code snippet (sub DownloadFile).

it's unclear that you are using the _OverrideUrl method correctly. @Erel has what he calls the
"smell test" for code. the code in that method makes me wonder what you're doing.
I cannot understand what you mean - the code of OverrideUrl is taken from this forum, and such lines are meant to download the file given in "link" string (see DownloadFile) - can they be improved?
thanks in advance
 
Upvote 0
Top