Android Question Accessing DCIM/Camera files

JackKirk

Well-Known Member
Licensed User
Longtime User
I am trying to set up access to list, read and delete files in DCIM/Camera

I have in Process_Globals:
B4X:
    Public treeUri As Uri
    Private ion As Object

In Main I have:
B4X:
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

Sub ion_Event(MethodName As String, Args() As Object) As Object
    Dim resultCode As Int = Args(0)
    Dim data As Intent = Args(1)

    If resultCode = -1 And data.IsInitialized Then
        Dim joIntent As JavaObject = data
        treeUri = joIntent.RunMethod("getData", Null)
                'Log("Selected folder URI: " & treeUri.ToString)

        ' Persist access
        Dim flags As Int = Bit.Or(1, 2)
        Dim jo As JavaObject
        jo.InitializeContext
        jo.RunMethod("takePersistableUriPermission", Array(treeUri, flags))
    Else
        Log("Picker cancelled or failed.")
    End If
    Return Null
End Sub

Public Sub Getfolder()

        Dim i As Intent
        i.Initialize("android.intent.action.OPEN_DOCUMENT_TREE", "")
        Log("Intent initialized: " & (i <> Null))


        Dim jo As JavaObject = i
        jo.RunMethod("addFlags", Array(Bit.Or(Bit.Or(1, 2), 8)))

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

Getfolder is called by a button click.

When it is called I get a dialog asking what folder I want - I hit the button and then nothing.

The ion_Event is never called.

Also GetBA returns an object with a null activity property.

I'm ripping my hair out and Copilot seems confused - or I am confusing it most likely.
 

drgottjr

Expert
Licensed User
Longtime User
try adding i.setType("*/*")
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
try adding i.setType("*/*")
I placed that after

jo.RunMethod("addFlags", Array(Bit.Or(Bit.Or(1, 2), 8)))

but it only had negative effects - the folder dialog no longer appeared.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
as near as i can tell, we're both drinking from the same source (except for the mime type, which you indicate you have added). i get the result i'm looking for. you seem to be looking for something a little different, but you should get at least as far as i do. sorry, i don't know what else to add.

B4X:
** Activity (main) Resume **
android.intent.action.MAIN
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
in ion: ResultArrived
back from result.  ok i reckon
2 args
ResultArrived
content://com.android.providers.media.documents/document/image%3A1000000659
this arg: -1
this arg: Intent { dat=content://com.android.providers.media.documents/document/image:1000000659 flg=0x43 }
** Activity (main) Resume **
android.intent.action.MAIN
mime type: image/jpeg
filename: PXL_20250628_220539798.MP.jpg
got 8152153 bytes
 

Attachments

  • 1.png
    1.png
    58.8 KB · Views: 33
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
I have it working - give me some time to tidy it up and I will post the solution
 
Upvote 0
Top