Android Question webviewExtra download file sdk 30

kkkpe

Active Member
Licensed User
Longtime User
With android 11 and sdk 30 the download of the file does not work ..
this is my code:
B4X:
Sub GetDownloadLink(link As String)
    
    Log("link:")
    Log(link)
    If link <> "" And link.StartsWith("http") Then
        Dim j As HttpJob
        j.Initialize("",Me)
        j.Download(link)
        ProgressDialogShow2("Downloading...",False)
        Wait For (j) JobDone(j As HttpJob)
        ProgressDialogHide
        If j.Success Then
            Dim filename As String = j.Response.GetHeaders.Get("content-disposition")
            Log(filename)
        
            If filename.IndexOf("filename=") <> -1 Then
            filename = filename.SubString2(filename.IndexOf("filename=")+9,filename.Length-1)
            Starter.nomefile = filename
            Dim os As OutputStream
            os = File.OpenOutput(File.DirRootExternal & "/Download",filename,False)
            File.Copy2(j.GetInputStream,os)
            os.Close
            Dim ext As String
            ext = filename.SubString2(filename.LastIndexof("."), filename.Length)
            Log("Estensione file:")
            Log(ext)
            If(ext = ".pdf") Then
                    Dim i As Intent               
                    File.Copy(File.DirRootExternal & "/Download/", filename, Starter.Provider.SharedFolder, filename)               
                    i.Initialize(i.ACTION_VIEW, "")
                    Starter.Provider.SetFileUriAsIntentData(i,filename)
                    'Type must be set after calling SetFileUriAsIntentData
                    i.SetType("application/pdf")
                    StartActivity(i)
            Else If(ext = ".pst") Then
                StartActivity(act_stampapos)
                
            End If

        End If
    End If
    End If
End Sub

The error is in the permissions to access the download folder .. How can I solve?
thank you.
 

agraham

Expert
Licensed User
Longtime User
You cannot directly access Download in Android 11. You will need to restructure your app to use permitted means of file access.

If you do not need to put your app in the the Play Store you can use this to access Download

Alternatively you can use one of these methods. ExternalStorage wpould let the user create/choose a sub-folder in Download but not access Download itself.
 
Upvote 0

Ivica Golubovic

Active Member
Licensed User
If you want to open the file after downloading, then you have to place the file in the shared folder using the file preovider class. If you just want to download the file without having to open it through your application, then you can download the file to the Download folder via the download manager for SDK30 (not older versions).
 
Upvote 0

kkkpe

Active Member
Licensed User
Longtime User
ok... Can you explain to me how the shared folder works? I read a little bit on the forum but it's a little confused..
 
Upvote 0

Ivica Golubovic

Active Member
Licensed User
ok... Can you explain to me how the shared folder works? I read a little bit on the forum but it's a little confused..
Follow the instructions and see examples from this link:
 
Upvote 0

Ivica Golubovic

Active Member
Licensed User
ok... Can you explain to me how the shared folder works? I read a little bit on the forum but it's a little confused..
Example::
Sub GetDownloadLink(link As String)
  
    Log("link:")
    Log(link)
    If link <> "" And link.StartsWith("http") Then
        Dim j As HttpJob
        j.Initialize("",Me)
        j.Download(link)
        ProgressDialogShow2("Downloading...",False)
        Wait For (j) JobDone(j As HttpJob)
        ProgressDialogHide
        If j.Success Then
            Dim filename As String = j.Response.GetHeaders.Get("content-disposition")
            Log(filename)
      
            If filename.IndexOf("filename=") <> -1 Then
            filename = filename.SubString2(filename.IndexOf("filename=")+9,filename.Length-1)
            Starter.nomefile = filename
            Dim os As OutputStream
            'os = File.OpenOutput(File.DirRootExternal & "/Download",filename,False)   Not allowed in SDK29 and SDK30
            os = File.OpenOutput(Starter.Provider.SharedFolder,filename,False)  'Alowed'
            File.Copy2(j.GetInputStream,os)
            os.Close
            Dim ext As String
            ext = filename.SubString2(filename.LastIndexof("."), filename.Length)
            Log("Estensione file:")
            Log(ext)
            If(ext = ".pdf") Then
                    Dim i As Intent             
                    'File.Copy(File.DirRootExternal & "/Download/", filename, Starter.Provider.SharedFolder, filename)             
                    i.Initialize(i.ACTION_VIEW, "")
                    Starter.Provider.SetFileUriAsIntentData(i,Starter.Provider.SharedFolder & "/" & filename) 'Or maybe Starter.Provider.SetFileUriAsIntentData(i, filename)
                    'Type must be set after calling SetFileUriAsIntentData
                    i.SetType("application/pdf")
                    StartActivity(i)
            Else If(ext = ".pst") Then
                StartActivity(act_stampapos)
              
            End If

        End If
    End If
    End If
End Sub
Try with this code...
 
Upvote 0
Top