Android Question Get the path of multiple images from the gallery

Status
Not open for further replies.

Esteve

Member
Licensed User
Hello
I would like to obtain the path of several images selected from the gallery.
I have tried the example of this thread with 4 random images.
B4X:
Sub Activity_Click
   Dim i As Intent
   i.Initialize("android.intent.action.GET_CONTENT", "")
   i.PutExtra("android.intent.extra.ALLOW_MULTIPLE", True)
   i.SetType("image/*")
   StartActivityForResult(i)
End Sub

Sub ion_Event (MethodName As String, Args() As Object) As Object
'Args(0) = resultCode
'Args(1) = intent
   If Args(0) = -1 Then 'resultCode = RESULT_OK
     Dim i As Intent = Args(1)
     Dim jo As JavaObject = i
     'Dim Uri1 As Uri
   
   
   End If
   Return Null
End Sub

Sub StartActivityForResult(i As Intent)
  Dim jo As JavaObject = GetBA
  ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
  jo.RunMethod("startActivityForResult", Array As Object(ion, i))
End Sub

Sub GetBA As Object
  Dim jo As JavaObject
  Dim cls As String = Me
  cls = cls.SubString("class ".Length)
  jo.InitializeStatic(cls)
  Return jo.GetField("processBA")
End Sub
I can see in mClipData.mItems the paths.

Cap1.PNG


I think that they might be obtained with GetPathFromContentResult but I don't know how do it.
Thanks.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code to get all the selected uris:
B4X:
Sub Process_Globals
   Private ion As Object
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")

End Sub

Sub Activity_Click
  Dim i As Intent
  i.Initialize("android.intent.action.GET_CONTENT", "")
  i.PutExtra("android.intent.extra.ALLOW_MULTIPLE", True)
  i.SetType("image/*")
  StartActivityForResult(i)
End Sub

Sub ion_Event (MethodName As String, Args() As Object) As Object
   If Args(0) = -1 Then 'resultCode = RESULT_OK
     Dim i As Intent = Args(1)
     Dim jo As JavaObject = i
     'Android 4.1+ (API 16)
     Dim uris As List
     uris.Initialize
     Dim clipdata As JavaObject = jo.RunMethod("getClipData", Null)
     If clipdata.IsInitialized Then
       Dim count As Int = clipdata.RunMethod("getItemCount", Null)
       For i2 = 0 To count -1
         Dim item As JavaObject = clipdata.RunMethod("getItemAt", Array(i2))
         uris.Add(item.RunMethod("getUri", Null))
       Next
     Else
       uris.Add(i.GetData)
     End If
     Log(uris)
   End If
   Return Null
End Sub

Sub StartActivityForResult(i As Intent)
  Dim jo As JavaObject = GetBA
  ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
  jo.RunMethod("startActivityForResult", Array As Object(ion, i))
End Sub

Sub GetBA As Object
  Dim jo As JavaObject
  Dim cls As String = Me
  cls = cls.SubString("class ".Length)
  jo.InitializeStatic(cls)
  Return jo.GetField("processBA")
End Sub

I guess that most users will not know that you need to long click to multiselect images, so it is probably worth mentioning it in your app.

Once you have the uri you can use the code from this snippet to get the actual file: https://www.b4x.com/android/forum/t...dia-files-returned-from-contentchooser.39313/ (if there is one).

It is also possible to open an input stream to the uri. Even if there is no real file.
 
Upvote 0
Status
Not open for further replies.
Top