Android Question ** ANSWERED **Education requested regarding MEDIA_SCANNER

RKM904

Member
Licensed User
Longtime User
I have embedded the camera example in a much larger app that is working well, but would like to better understand the android intent that forms part of the camera example.

the code (thank you Erel) segment shown below captures and stores the images within my app.

Sub Camera1_PictureTaken (Data() As Byte)
Dim dir As String = Main.photoDir
Dim filename As String = Main.photoID & ".jpg"
camera1.StartPreview
Dim out As OutputStream
out = File.Openoutput(dir,filename,False)
out.WriteBytes(Data, 0, Data.Length)
out.Close
Dim Phone As Phone
Dim i As Intent
i.Initialize("android.intent.action.MEDIA_SCANNER_SCAN_FILE", _
"file://" & File.Combine(dir, filename))
Phone.SendBroadcastIntent(i)
ToastMessageShow("Image saved: " & Main.photodir & Main.photofilename, True)
btnTakePicture.Enabled = True
End Sub

The sub above captures and stores the image(s) exactly as anticipated, however, the images are stored in exactly the same way with or without the 4 lines of code;

Dim i As Intent
i.Initialize("android.intent.action.MEDIA_SCANNER_SCAN_FILE", _
"file://" & File.Combine(dir, filename))
Phone.SendBroadcastIntent(i)

My question is; what exactly does the intent action MEDIA_SCANNER_SCAN_FILE do? In reading many of the forum questions and answers I thought (apparently incorrectly) that this would result in the folder where the images are stored being recognized as a folder by the Gallery application. This does not appear to be the case.

I've spent several hours in the b4a forums and other web sites but have only managed to become more confused. I would really appreciate an explanation (hopefully without disappearing down a technical rabbit hole) as to what exactly the media scan does and why it should be used in the camera example.

Thanks in advance.

P.S. as a newcomer to B4A and the forums I really appreciate the wealth of information and knowledge that is so willingly shared by the B4A community.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

RKM904

Member
Licensed User
Longtime User
The file will be saved properly with or without this intent.
This intent tells the "media scanner service" to scan this file. The result is that the file will appear if you connect it with a USB cable and that it should be added to the gallery. This intent will not work with new devices (4.3+).

There is a better solution which is posted here: http://www.b4x.com/android/forum/threads/force-media-scanner-on-deleted-file.25794/#post-214985

Thank you Erel for your prompt reply - I have changed the heading to ** ANSWERED **
 
Upvote 0
Top