Using the Media Scanner

If you have recorded new media of any kind, the MediaScannerConnection class provides a simple way for you to add it to the Media Store without needing to construct the full record for the Media Store Content Provider.

Before you can use the scanFile method to initiate a content scan on your file, you must call connect and wait for the connection to the Media Scanner to complete.

This call is asynchronous, so you will need to implement a MediaScannerConnectionClient to notify you when the connection has been made. You can use this same class to notify you when the scan is complete, at which point you can disconnect your Media Scanner Connection.

This sounds more complex than it is. Listing 11-20 shows the skeleton code for creating a new MediaScannerConnectionClient that defines a MediaScannerConnection which is used to add a new file to the Media Store.

LISTING 11-20: Adding files to the Media Store using the Media Scanner

Available for download on MediaScannerConnectionClient mediaScannerClient = new MediaScannerConnectionClient) {

private MediaScannerConnection msc = null;

msc = new MediaScannerConnection(getApplicationContext(), this); msc.connect();

public void onMediaScannerConnected() { msc.scanFile("/sdcard/test1.jpg", null);

public void onScanCompleted(String path, Uri uri) { msc.disconnect();

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide

Post a comment