Android Question [SOLVED] A robust way to check Gallery content in SDK 29+

JackKirk

Well-Known Member
Licensed User
Longtime User
As a follow on to:

https://www.b4x.com/android/forum/t...-erels-addbitmaptogallery.131374/#post-827725

which gives ability to put jpg/mp4 content in the Gallery in SDK 29+ devices...

I am looking for a way to be able to check if a user has subsequently deleted or moved the content in the Gallery on such 29+ devices and came up with this real simple possibility:
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
'    Private xui As XUI
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
End Sub

Sub Activity_Create(FirstTime As Boolean)

    CheckGallery("XYZalbum", "junk.jpg")

End Sub

Sub CheckGallery (AlbumName As String, TargetName As String)
       
    Dim rp As RuntimePermissions
    Dim root As String = rp.GetSafeDirDefaultExternal("")
    Log(root)
    root = root.SubString2(0, root.IndexOf("Android"))
    Log(root & "Pictures/" & AlbumName)
    Log(File.Exists(root & "Pictures/" & AlbumName, TargetName))
   
End Sub

I am concerned that this may not be a robust solution - taking the stub of rp.GetSafeDirDefaultExternal("") and using it to construct the full path to the content to be checked.

Will it hold good on devices other than my Pixel 3?

What about future Android releases?

Is there a more robust way to do it?

Attached is the zipped project.

Thanks for all and any feedback...
 

Attachments

  • CheckGallery.zip
    9.1 KB · Views: 205

JackKirk

Well-Known Member
Licensed User
Longtime User
After a lot more head scratching and discovering:

https://www.b4x.com/android/forum/t...-received-shared-file-example.117325/#content

I can pretty confidently say the answer to post #1 is NO

The robust(?) solution I have come up with is:
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
'    Private xui As XUI
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
End Sub

Sub Activity_Create(FirstTime As Boolean)

    CheckGallery("XYZalbum", "junk.jpg", "image/jpeg")
    Wait For CheckGallery_Complete_jpeg
    CheckGallery("XYZalbum", "junk.mp4", "video/mp4")
    Wait For CheckGallery_Complete_mp4
End Sub

Sub CheckGallery (AlbumName As String, TargetName As String, MimeType As String)
    Dim p As Phone
    Log(p.SdkVersion)
    If p.SdkVersion >= 29 Then
        Dim cr As ContentResolver
        cr.Initialize("cr")
        Dim MediaStore As JavaObject
        If MimeType = "image/jpeg" Then
            MediaStore.InitializeStatic("android.provider.MediaStore.Images$Media")
        Else
            MediaStore.InitializeStatic("android.provider.MediaStore.Video$Media")
        End If
        Dim EXTERNAL_CONTENT_URI As Uri = MediaStore.GetField("EXTERNAL_CONTENT_URI")
        Dim Cur As Cursor
        Cur = cr.query(EXTERNAL_CONTENT_URI, Array As String("_size"), "relative_path = ? AND _display_name = ?", Array As String("Pictures/" & AlbumName & "/", TargetName), "")
        Cur.Position = 0
        If Cur.GetString2(0) = Null Then
            Log(AlbumName & " " & TargetName & " does not exist")
        Else
            Log(AlbumName & " " & TargetName & " exists")
        End If
    End If
    If MimeType = "image/jpeg" Then
        CallSubDelayed(Me, "CheckGallery_Complete_jpeg")
    Else
        CallSubDelayed(Me, "CheckGallery_Complete_mp4")
    End If
End Sub
I was muddling around for quite some time before it dawned on me: ContentResolver creates an SQL database - after that everything made complete sense.

In my defence I don't muck around with databases much and it took me a while to recognize the "signature" in the code referenced at the top of this post.

Attached is a zip of the project.

Happy coding...
 

Attachments

  • CheckGallery2.zip
    9.4 KB · Views: 223
Last edited:
Upvote 0
Top