20.08.2016 Views

Professional Android 4 Application Development

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

662 x CHAPTER 15 AUDIO, VIDEO, AND USING THE CAMERA<br />

More than 25 effects are currently supported. The full list — including the parameters they<br />

support — is available at http://developer.android.com/reference/android/media/effect/<br />

EffectFactory.html.<br />

After configuring the effect you want to apply, use its apply method, passing in the input texture, its<br />

dimensions, and the target texture to apply it.<br />

ADDING MEDIA TO THE MEDIA STORE<br />

By default, media files created by your application that are stored in private application folders will<br />

be unavailable to other applications. To make them visible, you need to insert them into the Media<br />

Store. <strong>Android</strong> provides two options for this. The preferred approach is to use the Media Scanner<br />

to interpret your file and insert it automatically. Or you can manually insert a new record in the<br />

appropriate Content Provider. Using the Media Scanner is almost always the better approach.<br />

Inserting Media Using the Media Scanner<br />

If you have recorded new media of any kind, the MediaScannerConnection class provides the<br />

scanFile method as a simple way for you to add it to the Media Store without needing to construct<br />

the full record for the Media Store Content Provider.<br />

Before you can use the scanFile method to initiate a content scan on your file, you must call<br />

connect and wait for the connection to the Media Scanner to complete. This call is asynchronous,<br />

so you will need to implement a MediaScannerConnectionClient to notify you when the connection<br />

has been made. You can use this same class to notify you when the scan is complete, at which<br />

point you can disconnect your Media Scanner Connection.<br />

This sounds more complex than it is. Listing 15-34 shows the skeleton code for creating a new<br />

MediaScannerConnectionClient that defines a MediaScannerConnection, which is used to add a<br />

new file to the Media Store.<br />

LISTING 15-34: Adding files to the Media Store using the Media Scanner<br />

private void mediaScan(final String filePath) {<br />

MediaScannerConnectionClient mediaScannerClient = new<br />

MediaScannerConnectionClient() {<br />

private MediaScannerConnection msc = null;<br />

{<br />

}<br />

msc = new MediaScannerConnection(<br />

VideoCameraActivity.this, this);<br />

msc.connect();<br />

public void onMediaScannerConnected() {<br />

// Optionally specify a MIME Type, or<br />

// have the Media Scanner imply one based<br />

// on the filename.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!