Android Question [B4A] Downloading a file knowing URI

Aldo's

Active Member
Licensed User
Using a content chooser I extract a URI of a file present either on my device or on Google Drive.
How do I download this file and insert it into File.DirInternal?
 

walt61

Well-Known Member
Licensed User
Longtime User
B4X:
    Dim cc As ContentChooser
    cc.Initialize("CC")
    cc.Show("*/*", "Select any file")
    Wait For CC_Result (Success As Boolean, Dir As String, fileURI As String)
    If Success Then
        Try
            Dim OutStr As OutputStream = File.OpenOutput(File.DirInternal,"myfile",False)
            Dim InStr As InputStream = File.OpenInput("ContentDir",fileURI)
            File.Copy2(InStr,OutStr)
            OutStr.Close
        Catch
            xui.MsgboxAsync(LastException, "B4X")
        End Try        
        xui.MsgboxAsync("Done", "B4X")
    Else
        xui.MsgboxAsync("ContentChooser failed or was cancelled", "B4X")
    End If

EDIT: my original code failed (at least on TargetSDK 34); I replaced it with the code from https://www.b4x.com/android/forum/t...-downloads-folder-with-contentchooser.122785/, which does work
 
Last edited:
Upvote 0

Aldo's

Active Member
Licensed User
I tried with the following code (which requires runtime permissions):
B4X:
rivate Sub Button1_Click
    Dim cc As ContentChooser
    cc.Initialize("CC")
    cc.Show("*/*", "Select any file")
    Wait For CC_Result (Success As Boolean, Dir As String, sFN1 As String)
    If Success Then
        Dir = Dir.Replace("%3A", ":").Replace("%2F", "/")
        Log(Dir)
        sFN1 = sFN1.Replace("%3A", ":").Replace("%2F", "/")
        sFN=sFN1
        Log(sFN)
        rp.CheckAndRequest(rp.PERMISSION_READ_EXTERNAL_STORAGE)
        Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
        If Result Then
            File.Copy(Dir, sFN, File.DirInternal, sFN)
        Else
            Log("NO PERMISSION")
        End If
        lblPath.As(Label).Text= Dir & " " & sFN
    Else
        Log("ContentChooser failed or was cancelled")
    End If
End Sub
But in Logs it gives me "NO PERMISSION".
The app does not crash.
What am I doing wrong?
 
Upvote 0
Top