Android Question Show image in Gallery (Android 7.0)

Discussion in 'Android Questions' started by GGSoft, Sep 27, 2018.

  1. GGSoft

    GGSoft Member Licensed User

    After searching in the forum, I tried this code:
    Code:
    ' 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, NullNull))
    End If
    It works fine in Androd 4.4- but not in Android 7.0

    Can anyone give me an advice, please?
     
  2. DonManfred

    DonManfred Expert Licensed User

  3. GGSoft

    GGSoft Member Licensed User

    Attached Files:

  4. Erel

    Erel Administrator Staff Member Licensed User

    What happens when it doesn't work? Do you see any error?
     
  5. GGSoft

    GGSoft Member Licensed User

    There is no error. After the execution the image is not shown in the gallery.
     
  6. GGSoft

    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)
    Code:
    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)
    Code:
    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, NullNull))
    Could not test for API 24 (Android 7.0) through FileProvider.

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

    Erel Administrator Staff Member Licensed User

    This code works:
    Code:
    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
     
  8. GGSoft

    GGSoft Member Licensed User


    Awesome (already tested). Many thanks, Erel. Somehow it's a relief to know that nobody but the wisest man could help me. : )
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice