Android Question get image from a video

Lucas Eduardo

Active Member
Licensed User
Hello, i am trying to get image from a video. I did it when i recorded a video and went back to app, but when i use the ContentChooser i can't do this.
How can i get the image from a video after i choose this image with ContentChooser?

Here is my code to get the image after i record a video:
B4X:
video.Initialize("video") 'video is a process global VideoRecordApp variable.
        Dim folder As String = Starter.Provider.SharedFolder
        Dim FileName As String = "1.mp4"
        video.Record3(folder, FileName, -1, Starter.Provider.GetFileUri(File.Combine(folder, FileName)))
        Wait For Video_RecordComplete (Success As Boolean)
        If Success Then
            'Example of playing the recorded video
            Dim vidfile As String = File.Combine(folder, FileName)
            Dim rfl As Reflector
            Dim obj As Object
            Dim b As Bitmap
     
            obj = rfl.CreateObject("android.media.MediaMetadataRetriever")
            rfl.Target = obj
            rfl.RunMethod2("setDataSource", vidfile, "java.lang.String")
            b = rfl.RunMethod3("getFrameAtTime", 0, "java.lang.long", 3, "java.lang.int")
   
            icVideoMediaAtividadeSuspeitaSvBullying.Bitmap = b
           
            ToastMessageShow("Vídeo recorded with success!", True)
        End If


and code to choose an video

B4X:
Dim cc As ContentChooser
        cc.Initialize("cc")
        cc.show("video/*", "Choose video")
        Wait For cc_Result(Success As Boolean, Dir As String, FileName As String)
        If Success Then
            Log("vídeo choosed- Folder: " & Dir & "  file name: "&FileName)
            ToastMessageShow("Video choosed with success!", True)
   
        Else
            ToastMessageShow("Video not selected!", True)
        End If


Thanks.
 

Lucas Eduardo

Active Member
Licensed User
the output is /storage/emulated/0/Android/data/app.sao.luis/files/shared/1.mp4

but i can get the image with this code
B4X:
Dim vidfile As String = File.Combine(folder, FileName)
            Dim rfl As Reflector
            Dim obj As Object
            Dim b As Bitmap
     
            obj = rfl.CreateObject("android.media.MediaMetadataRetriever")
            rfl.Target = obj
            rfl.RunMethod2("setDataSource", vidfile, "java.lang.String")
            b = rfl.RunMethod3("getFrameAtTime", 0, "java.lang.long", 3, "java.lang.int")
   
            icVideoMediaAtividadeSuspeitaSvBullying.Bitmap = b

the problem is when i use the code to choose a video. How can i get the image?

the output of log
B4X:
Log("video choosed- Folder: " & Dir & "  file name: "&FileName)

is
B4X:
 video choosed- Folder: ContentDir  file name: content://com.android.providers.media.documents/document/video%3A3979
 
Upvote 0

Lucas Eduardo

Active Member
Licensed User
i got it

here is the code

B4X:
Dim cc As ContentChooser
        cc.Initialize("cc")
        cc.show("video/*", "Choose video")
        Wait For cc_Result(Success As Boolean, Dir As String, FileName As String)
        If Success Then
           
            ToastMessageShow("Video choosed with success!", True)
            Try
                Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_READ_EXTERNAL_STORAGE)
                Wait For Activity_PermissionResult (Permission As String, ResultCkeck As Boolean)
                If ResultCkeck Then
                    Dim folder As String = Starter.Provider.SharedFolder
                    Dim newFileName As String = "1.mp4"
                   
                    Dim out As OutputStream
                    Dim input As InputStream
                    input = File.OpenInput(Dir , FileName)
                    out = File.OpenOutput(folder, newFileName , False )
                    File.Copy2(input, out)
                    out.Close
                   
                    'Log(File.Size(folder, newFileName))
                    Dim vidfile As String = File.Combine(folder, newFileName)
                   
                    Dim rfl As Reflector
                    Dim obj As Object
                    Dim b As Bitmap
     
                    obj = rfl.CreateObject("android.media.MediaMetadataRetriever")
                    rfl.Target = obj
                    rfl.RunMethod2("setDataSource", vidfile, "java.lang.String")
                    b = rfl.RunMethod3("getFrameAtTime", -5, "java.lang.long", 2, "java.lang.int")
                   
                    Dim i, j As Int
                   
                    i = b.Height * 0.3
                    j = b.Width * 0.5
               
                    icVideoMediaAtividadeSuspeitaSvBullying.Bitmap = b.Resize(j,i,True)
                   
                   
                End If
               
               
            Catch
                Log(LastException)
            End Try
   
        Else
            ToastMessageShow("The video wasn't selected!", True)
        End If
 
Upvote 1
Top