Android Question Show image in Gallery (Android 7.0)

GGSoft

Member
Licensed User
After searching in the forum, I tried this code:
B4X:
' The image
Dim bitmap As Bitmap = LoadBitmap(File.DirAssets, "prueba.jpg")

' Copy to ...files/pruebas/prueba.jpg
Dim Out As OutputStream
Dim r As RuntimePermissions
Out = File.OpenOutput(r.GetSafeDirDefaultExternal("pruebas"), "prueba.jpg", False)
bitmap.WriteToStream(Out, 100, "JPEG")
Out.Close

' "Broadcast" image to gallery
Dim Phone As Phone
If Phone.SdkVersion < 19 Then
    ' This works            :)
    Dim i As Intent
    'i.Initialize("android.intent.action.MEDIA_MOUNTED", "file://" & File.Combine(r.GetSafeDirDefaultExternal("pruebas"), "prueba.jpg"))
    i.Initialize("android.intent.action.MEDIA_SCANNER_SCAN_FILE", "file://" & File.Combine(r.GetSafeDirDefaultExternal("pruebas"), "prueba.jpg"))
    Phone.SendBroadcastIntent(i)
Else
    ' This does not work    :(
    Dim context As JavaObject
    context = context.InitializeStatic("anywheresoftware.b4a.BA").GetField("applicationContext")
    Dim paths() As String = Array As String(File.Combine(r.GetSafeDirDefaultExternal("pruebas"), "prueba.jpg")) 'files to scan
    Dim mediaScanner As JavaObject
    mediaScanner.InitializeStatic("android.media.MediaScannerConnection")
    mediaScanner.RunMethod("scanFile", Array As Object(context, paths, Null, Null))
End If
It works fine in Androd 4.4- but not in Android 7.0

Can anyone give me an advice, please?
 

GGSoft

Member
Licensed User
I almost give up.

From this post I thought it would be easy to show in the images gallery (not open directly, but show the image and then open if desired) any image I get downloaded from a server and saved in RunTimePermissions.GetSafeDirDefaultExternal("foo").

This works (Android 4.3.1-) API 18- (The image is shown in the Gallery)
B4X:
Dim Phone as phone
Dim i As Intent
i.Initialize("android.intent.action.MEDIA_MOUNTED", "file://" & File.Combine(dir2, filename))
Phone.SendBroadcastIntent(i)
This doesn't work (Android 4.4.2 -- 6.0) 19 <= API <= 23 (The image is not shown in the Gallery)
B4X:
Dim context As JavaObject
context = context.InitializeStatic("anywheresoftware.b4a.BA").GetField("applicationContext")
Dim paths() As String = Array As String(File.Combine(dir2,filename)) 'files to scan
Dim mediaScanner As JavaObject
mediaScanner.InitializeStatic("android.media.MediaScannerConnection")
mediaScanner.RunMethod("scanFile", Array As Object(context, paths, Null, Null))
Could not test for API 24 (Android 7.0) through FileProvider.

Isn't any way, similar or analogous, to this (B4i) in B4A?
 

Erel

Administrator
Staff member
Licensed User
This code works:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim rp As RuntimePermissions
   rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
   Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
   If Result Then
       File.Copy(File.DirAssets, "prueba.jpg", File.DirRootExternal, "Pictures/prueba.jpg")
       Dim FilePath As String = File.Combine(File.DirRootExternal, "Pictures/prueba.jpg")
       Dim Phone As Phone
       If Phone.SdkVersion <= 18 Then           ' min - 4.3.1
           Dim i As Intent
           i.Initialize("android.intent.action.MEDIA_SCANNER_SCAN_FILE", "file://" & FilePath)
           Phone.SendBroadcastIntent(i)
       Else
           Dim ctxt As JavaObject
           ctxt.InitializeContext
           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(ctxt, Array As String(FilePath), Array As String("image/jpeg"), interface))
       End If
   End If

End Sub

Sub ScanCompleted_Event (MethodName As String, Args() As Object) As Object
   Log(Args(0))
   Log(Args(1))
   Return Null
End Sub
 

GGSoft

Member
Licensed User
This code works:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim rp As RuntimePermissions
   rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
   Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
   If Result Then
       File.Copy(File.DirAssets, "prueba.jpg", File.DirRootExternal, "Pictures/prueba.jpg")
       Dim FilePath As String = File.Combine(File.DirRootExternal, "Pictures/prueba.jpg")
       Dim Phone As Phone
       If Phone.SdkVersion <= 18 Then           ' min - 4.3.1
           Dim i As Intent
           i.Initialize("android.intent.action.MEDIA_SCANNER_SCAN_FILE", "file://" & FilePath)
           Phone.SendBroadcastIntent(i)
       Else
           Dim ctxt As JavaObject
           ctxt.InitializeContext
           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(ctxt, Array As String(FilePath), Array As String("image/jpeg"), interface))
       End If
   End If

End Sub

Sub ScanCompleted_Event (MethodName As String, Args() As Object) As Object
   Log(Args(0))
   Log(Args(1))
   Return Null
End Sub

Awesome (already tested). Many thanks, Erel. Somehow it's a relief to know that nobody but the wisest man could help me. : )
 
Top