Android Question How can I copy file selected from a ContentChooser into DirInternal?

PhrotonX

Member
I've been trying to obtain a file selected from a ContentChooser, but I do not have any idea on how I could get such file based on a Uri.


B4X:
Private Sub btnAddAttachment_Click
    Private filepicker As ContentChooser
   
    filepicker.Initialize("filepicker")
    filepicker.Show("*/*", "Choose file")
End Sub

Private Sub filepicker_Result (Success As Boolean, Dir As String, FileName As String)
    If Success Then
        ' will definitely not work.
        File.Copy(Dir, FileName, File.DirInternal, FileName)

    Else
        MsgboxAsync("Unable to retrieve attachment", "Error")
    End If
End Sub

End Sub
 

walt61

Well-Known Member
Licensed User
Longtime User
I've also updated the code in https://www.b4x.com/android/forum/threads/b4a-downloading-a-file-knowing-uri.166397/#post-1020388 as it appeared to fail; this works (just tested it):
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
 
Upvote 0
Top