Forcing media to be rescanned?

hackhack

Active Member
Licensed User
Longtime User
If i copy a file to the device, the gallery doesn't automatically notice this.

Anybody know how to trigger this rescan programmatically? (You can switch of the device and it will rescan at boot time, but that's hardly optimal)
 

thedesolatesoul

Expert
Licensed User
Longtime User
If you can build an intent, you can start the media scanner.

public static final String ACTION_MEDIA_SCANNER_SCAN_FILE

Since: API Level 1
Broadcast Action: Request the media scanner to scan a file and add it to the media database. The path to the file is contained in the Intent.mData field.
Constant Value: "android.intent.action.MEDIA_SCANNER_SCAN_FILE"
From: Intent | Android Developers

Build an intent in B4a using: Basic4android - Phone

I havent actually done this though.
 
Upvote 0

hackhack

Active Member
Licensed User
Longtime User
Actually what I meant, and I know this was imprecise, is: "After i have copied file with third party apps the media database isn't updated) - is there a way update all files" :)

So if I were to write an app to do this, does that mean i have to scan all files and run the intent for each file?

Or is there one which starts a complete rescan? ACTION_MEDIA_SCANNER_STARTED doesn't sound like its doing that.
 
Upvote 0

hackhack

Active Member
Licensed User
Longtime User
One thing which has me confused right away though is how to set the intent.

Shouldn't it be
Intent1.Initialize(Intent1.ACTION_MEDIA_SCANNER_SCAN_FILE, "")

But it doesn't seem you can set more than 5 basic actions

Also, how does one set "mData" Hm... ponder (need bigger docs ;)
 
Last edited:
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
This looks harder than I thought.

Is it possible to do: Intent1.ACTION_MEDIA_SCANNER_SCAN_FILE

Im not at my computer so Im not sure.
Also how do we set the component? Do we have something like com.google.android.mediascanner? Or do we not need to set anything.
Setting mData would probably be through PutExtra.

i'll try when i get the time, keep me posted if you get anything.
Also just wondering if B4A implements a BroadcastReceiver (although that has nothing to do with this topic.)
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
There are two primary forms of intents you will use.

Explicit Intents have specified a component (via setComponent(ComponentName) or setClass(Context, Class)), which provides the exact class to be run. Often these will not include any other information, simply being a way for an application to launch various internal activities it has as the user interacts with the application.

Implicit Intents have not specified a component; instead, they must include enough information for the system to determine which of the available components is best to run for that intent.

Maybe we need to find the component name...if there is one.

Also, if we look through the xml files for the system apps we should be able to find which component is listening for ACTION_MEDIA_SCANNER_SCAN_FILE

Frankly, I am out of my depth, because on Java, this is a top-level intent i.e. the android system can understand this, but it seems it is not available in B4A.
On the other hand it would be easy to write a library for this (which i know nothing about!)
 
Upvote 0
Top