Recipe Adding Media and Updating Paths

After an application creates a newly recorded audio file, it can be registered with the system as available for use.This is done using the MediaStore class. For example, after an audio file myFile is saved, Listing 6.11 shows how to register it as a possible ringtone, notification, and alarm, but not to be seen by an MP3 player (because IS_MUSIC is false).

Listing 6.11 Example of Registering an Audio File to the System

//reload MediaScanner to search for media and update paths sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"

+ Environment.getExternalStorageDirectory()))); ContentValues values = new ContentValues();

values.put(MediaStore.MediaColumns.DATA, myFile.getAbsolutePath()); values.put(MediaStore.MediaColumns.TITLE, myFile.getName()); values.put(MediaStore.MediaColumns.TIMESTAMP,

System.currentTimeMillis()); values.put(MediaStore.MediaColumns.MIME_TYPE, recorder.getMimeContentType()); values.put(MediaStore.Audio.Media.ARTIST, SOME_ARTIST_HERE); values.put(MediaStore.Audio.Media.IS_RINGTONE, true); values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true); values.put(MediaStore.Audio.Media.IS_ALARM, true); values.put(MediaStore.Audio.Media.IS_MUSIC, false); ContentResolver contentResolver = new ContentResolver(); Uri base = MediaStore.Audio.INTERNAL_CONTENT_URI; Uri newUri = contentResolver.insert(base, values); String path = contentResolver.getDataFilePath(newUri);

Here ContentValues is used to declare some standard properties of the file, such as TITLE, TIMESTAMP, and MIME_TYPE, and ContentResolver is used to create an entry in the MediaStore content database with the file's path automatically added.

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Post a comment