即使媒体保存在文件中(正如录制情况),媒体文件对于其他应用也是不可用的。要指定文件,必须把该文件插入到MediaStore中。MediaStore是个内容提供者,用来通过设备保存和检索媒体数据(图像、视频及音频)。要保存文件引用,创建ContentValues对象,把它插入到合适的MediaStore内容提供者中。以下例子在音频文件中插入合适的元数据,如标题和艺术家:
// generate ContentValues and add appropriate metadata values ContentValues content = new ContentValues; // VERY IMPORTANT! Must reference the absolute path of the data. content.put(MediaStore.MediaColumns.DATA, "/sdcard/AudioExample.3gpp"); content.put(MediaStore.MediaColumns.TITLE, "AudioRecordExample"); content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/amr"); content.put(MediaStore.Audio.Media.ARTIST, "Me"); content.put(MediaStore.Audio.Media.IS_MUSIC, true); // get the Content Resolver ContentResolver resolve = getContentResolver; // insert into the content resolver Uri uri = resolve.insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, content); // announce to everyone that cares that it was inserted sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));