Android Question Save photo in gallery

JustinLong

Member
Licensed User
I'm new to B4A and am having an issue that is driving me nuts.

I'm trying to take a picture then save it to the gallery. This doesn't seem like it should be this hard.

I'm currently using the code from this thread.

The picture will save in the directory I set, but it never shows up in the gallery.

Here's the code i'm using:

B4X:
Sub cam_PictureTaken (Data() As Byte)
    filename = DateTime.Now & ".jpg"
    dir = Starter.rp.GetSafeDirDefaultExternal("")
  
    Cam.SavePictureToFile(Data, dir, filename)
    Cam.StartPreview 'restart preview
  
End Sub




Sub continuebtn_Click
    Try  
        If p.SdkVersion<19 Then
      
            Dim i As Intent
            i.Initialize("android.intent.action.MEDIA_SCANNER_SCAN_FILE", "file://" & File.Combine(dir, filename))
            p.SendBroadcastIntent(i)
        Else
           
            Dim context As JavaObject
            context = context.InitializeStatic("anywheresoftware.b4a.BA").GetField("applicationContext")
            Dim paths() As String = Array As String(File.Combine(dir,filename)) 'files to scan
            Dim mediaScanner As JavaObject
            mediaScanner.InitializeStatic("android.media.MediaScannerConnection")
            mediaScanner.RunMethod("scanFile", Array As Object(context, paths, Null, Null))
        End If
        Log("saved")
    Catch
        Log(LastException)
    End Try

End Sub

I currently have the targetSdkVersion="26" in the Manifest editor.
 

JustinLong

Member
Licensed User

Thank you so much - this worked wonderfully. Reposting the code that worked for me below:

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
 
Upvote 0
Top