Android Question Bizarre behaviour by android.media.MediaScannerConnection

JackKirk

Well-Known Member
Licensed User
Longtime User
I am using following:
B4X:
            'Associate file with gallery
            If wrk_phn.SdkVersion < 19 Then
                wrk_int.Initialize("android.intent.action.MEDIA_MOUNTED", "file://" & File.Combine(File.DirRootExternal & "/" & Gen_photo_album, wrk_str & ".jpg"))
                wrk_phn.SendBroadcastIntent(wrk_int)
            Else
                wrk_jo = wrk_jo.InitializeStatic("anywheresoftware.b4a.BA").GetField("applicationContext")
                wrk_pth = Array As String(File.Combine(File.DirRootExternal & "/" & Gen_photo_album, wrk_str & ".jpg"))
                wrk_mediaScanner.InitializeStatic("android.media.MediaScannerConnection")
                wrk_mediaScanner.RunMethod("scanFile", Array As Object(wrk_jo, wrk_pth, Null, Null))
            End If
based on code I found in the forums, under Erel's hand, to force certain files to appear in the photo gallery.

Unfortunately it seems to work too well.

Once you have run this code once - and the app has been dismissed - even uninstalled - any .jpg file that is saved in File.DirRootExternal by any means, including manually copying from the PC via USB, will appear in photo gallery.

It appears some sort of permanent connection is made that survives power down/up, battery removal etc - only thing I haven't done is a factory reset.

This is obviously not good app behaviour - anyone with an idea of how to stop it?
 

DonManfred

Expert
Licensed User
Longtime User
his is obviously not good app behaviour
It is the Android behaviour, not the apps...

Create a file named .nomedia ([dot]nomedia). Is far as i remember folders with this file in are not scanned/ignored

I mean; put this file to any folder you want to ignore in the mediascan

See https://developer.android.com/guide/topics/data/data-storage.html

Hiding your files from the Media Scanner
Include an empty file named .nomedia in your external files directory (note the dot prefix in the filename). This prevents media scanner from reading your media files and providing them to other apps through the MediaStore content provider. However, if your files are truly private to your app, you should save them in an app-private directory.
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
DonManfred,

Thanks for your response.

Initially my response to yours was "????" (well in the Australian vernacular it was a good bit coarser:)).

But it made me do a lot of googling which made me realise I didn't have a clue what the media scanner was or what the code in my first post was doing.

I eventually came across a one line summary of what the media scanner does:

The media scanner just scans through all of the files on external storage, collecting meta-data about them to populate the media database so you can do queries on them.

Simple and authoritative - well it should be as it was written by a "Android framework engineer".

Other reading suggests this scanning process can lag generation of new media files like .jpg, .mp3 etc

So theoretically if a .jpg is filed on File.DirRootExternal sooner or later the media scanner will find it and add it to the photo gallery.

What the code in the first post does is just removes the "sooner or later" element from the previous sentence.

So the code in the first post has nothing to do with the "bizarre behaviour" I thought I was observing - in fact that "bizarre behaviour" was just the media scanner doing what it was supposed to do.

And your comments about using .nomedia blank files to vary that behaviour are well worth noting strongly.

Thanks again - I am better informed and content - at least on this front...
 
Upvote 0
Top