Android Question Image to Gallery

carlos7000

Active Member
Licensed User
Hi.

I want an image to appear in the gallery. He has used Erel's code, but it doesn't work for me.

The image is copied correctly to the Pictures folder, but does not appear in the gallery.

I thought the problem could be because the images on my device are saved in DCIM/Camera. Update the code, the image was copied correctly, but it still does not appear in the gallery.

The code I use is this

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, "gatito.jpg", File.DirRootExternal, "Pictures/gatito.jpg")
       Dim FilePath As String = File.Combine(File.DirRootExternal, "Pictures/gatito.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
The code for DCIM was

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, "gatito.jpg", File.DirRootExternal, "DCIM/Camera/gatito.jpg")
       Dim FilePath As String = File.Combine(File.DirRootExternal, "DCIM/Camera/gatito.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

It also copies the file, but it does not appear in the gallery

Attach the project

Thanks
 

Attachments

Erel

Administrator
Staff member
Licensed User
I've ran your program.

These are the logs:
Logger connected to: Google Pixel 2
--------- beginning of main
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
/storage/emulated/0/Pictures/gatito.jpg
content://media/external_primary/images/media/1716

I see the nice picture in Photos, under the the Pictures album.
 
Top