讀古今文學網 > Android程序設計:第2版 > 存儲的媒體內容 >

存儲的媒體內容

即使媒體保存在文件中(正如錄製情況),媒體文件對於其他應用也是不可用的。要指定文件,必須把該文件插入到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));