Android Question How to remove image from Gallery Android 5 - 10+?

lymey

Active Member
Licensed User
Longtime User
Hi Erel, this is what I came up with:
B4X:
                  If ph.SdkVersion < 29 Then
                      Try
                        File.Delete(chosen_directory, chosen_filename)
                        'mediascanner for sdk < 29
                        Dim context As JavaObject
                        context.InitializeContext
                        Dim MimeType As String
                        If chosen_filename.Contains(".jpg") Or chosen_filename.Contains(".jpeg") Then
                            MimeType="image/jpg"
                        End If
                        If chosen_filename.Contains(".png")  Then
                            MimeType="image/png"
                        End If
                        Dim FilePath As String = File.Combine(chosen_directory,chosen_filename)
                        Dim MediaScannerConnection As JavaObject
                        MediaScannerConnection.InitializeStatic("android.media.MediaScannerConnection")
                        Dim interface As Object = MediaScannerConnection.CreateEventFromUI("android.media.MediaScannerConnection.OnScanCompletedListener", "ScanCompleted", _
                        Null)
                        MediaScannerConnection.RunMethod("scanFile", Array(context, Array As String(FilePath), Array As String(MimeType), interface))
                        Wait For ScanCompleted_Event (MethodName As String, Args() As Object)
                  
                        Catch
                          Log(LastException.Message)
                          Wait For (xui.MsgboxAsync("Sorry, the file/thumbnail can't be removed!", "Move From Gallery")) Msgbox_Result (mResult As Int)
                          Return
                        End Try
                    Else
                        Try
                        'SdkVersion >= 29 Then
                        Dim cr As ContentResolver
                        cr.Initialize("cr")
                        Dim MediaStoreImagesMedia As JavaObject
                        MediaStoreImagesMedia.InitializeStatic("android.provider.MediaStore.Images$Media")
                        Dim EXTERNAL_CONTENT_URI As Uri = MediaStoreImagesMedia.GetField("EXTERNAL_CONTENT_URI")
                        cr.Delete(EXTERNAL_CONTENT_URI, "_display_name = ?", Array As String(chosen_filename))
                        If File.Exists(chosen_directory, chosen_filename) Then
                            Log("SDK 29 gallery delete failed")
                        End If
                        
                        Catch
                            Log("SDK 29 ERROR: " & LastException.Message)
                            Wait For (xui.MsgboxAsync("Sorry, the file/thumbnail can't be removed!", "Move From Gallery")) Msgbox_Result (mResult As Int)
                            Return
                        End Try
                    End If
 
Upvote 0
Top