Android Question Well-trusted way to get multi-image

red30

Well-Known Member
Licensed User
Longtime User
I used the code from this thread:
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
It doesn't work on some devices.
On one device it doesn't work when choosing one or several images (one weights ~Mb) in ion_Event - clipdata.IsInitialized=False. On another, choosing any number of images, always clipdata.IsInitialized=False. Why? Both Androids are > 4.3. Is there another way to get multi-images?
 

DonManfred

Expert
Licensed User
Longtime User
It doesn't work on some devices.
It does not work on which Devices? Especially which Android Version are these devices running?
Note: the EXTRA_ALLOW_MULTIPLE option is only available in Android API 18 and higher.
But you said they are using Androids > 4.3 (18). So it should work i guess.

I guess it depends on the installed Gallery-App somehow. But i´m not sure. Some Manufacturers may have different inplementations.
 
Last edited:
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
The first device Android 4.4.2
The second device Android 5.1.1
I found this example:
B4X:
Sub Globals
    Private ImageList As List
    Private OldIntent As Intent
End Sub
Sub Button2_Click
    ImageList=GetUrisFromStartingIntent(Activity.GetStartingIntent)
End Sub
Private Sub GetUrisFromStartingIntent(in As Intent) As List
    Log(in.Action)
    Log(in.GetData)
    Log(in.ExtrasToString)
    Dim ImageList As List
    ImageList.Initialize
    If in.IsInitialized And in <> OldIntent Then
        If in.Action = "android.intent.action.SEND" Or in.Action= "android.intent.action.SEND_MULTIPLE" Then
           ImageList= GetImageUrisFromExtras(in.ExtrasToString, in.action)    
        End If
        OldIntent = in
    End If
    Return ImageList
End Sub

Sub GetImageUrisFromExtras (Extras As String, Intent As String) As List
    Log(Extras)
    Dim ImageList As List
    ImageList.Initialize
   
    Dim JO As JavaObject = Activity.GetStartingIntent
    Select Intent
        Case "android.intent.action.SEND"
            Dim uri As String = JO.RunMethod("getParcelableExtra", Array("android.intent.extra.STREAM"))
            ImageList.Add(uri)
        Case "android.intent.action.SEND_MULTIPLE"
           
            Dim uris As List = JO.RunMethod("getParcelableArrayListExtra", Array("android.intent.extra.STREAM"))
            If uris.Size > 0 Then
                For i = 0 To uris.Size-1
                    ImageList.Add(uris.Get(i))
                Next
            End If
    End Select
   
    Return ImageList
End Sub
What am I doing wrong?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Does not look like you are providing enough informations.
What is the content of the result (add log). what is the size of the uris? what are the values you put on the list (add logs)
Log(in.Action)
Log(in.GetData)
Log(in.ExtrasToString)
What is the output?
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
I did not understand. This example is to select images from the gallery and click "Share". And I need to select images from the application.
 
Last edited:
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
When the program moves here:
Dim clipdata As JavaObject = jo.RunMethod("getClipData", Null)
clipdata=Not initialized.
If clipdata.IsInitialized Then = false.
Why does it happen on some devices? How can i fix it?
 
Upvote 0
Top