Android Question Copy file from uri [SOLVED]

carlos7000

Well-Known Member
Licensed User
Longtime User
Hello.

I am trying to create an application, which through the share menu, receives a uri of a file, then the application copies it to a "safe" directory

I already managed to receive the Uri correctly.

I also managed to correctly extract the path of the file, from the uri

But the copy function says that the origen file cannot be found.

I found this https://www.b4x.com/android/forum/threads/copy-file-from-uri.90300/ that he sent me to https://www.b4x.com/android/forum/threads/receiving-shared-images-from-other-apps.81041/ but it doesn't give me any clue to copy the file. or I did not understand.

This is the code

B4X:
Sub Activity_Resume
    If IsRelevantIntent(Activity.GetStartingIntent) Then
        Dim in As JavaObject = Activity.GetStartingIntent
        Dim uri As String = in.RunMethod("getParcelableExtra", Array("android.intent.extra.STREAM"))
        Log("Uri " & uri)
       
        Try
            Dim FileName As String
            FileName = Uri_Class.GetFileInfoByIndex("_display_name", uri) 'Get the file name
            Log("Name " & FileName)
           
            Dim OriginPath As String
            OriginPath = GetPathUri(False, uri) 'Get the file path
            Log("OriginPath1 " & OriginPath)
           
            Dim SafeFolder As String
            SafeFolder = "SafeFolder"
           
            Dim Destination As String = "file:///" & SafeFolder
            Log("Destinarion " & Destination)
           
            'OriginPath = OriginPath.SubString2(0, OriginPath.LastIndexOf("/"))
            'Log("OriginPath1 " & OriginPath)
           
            File.Copy(OriginPath, FileName, File.DirInternal, FileName)
            Log("ok")
        Catch
            Log(LastException)
        End Try
    End If
End Sub
 
Last edited:
Top