我正在尝试将我的音频文件添加到MediaStore,但我似乎无法找到将歌曲添加到现有专辑的方法。我尝试在添加歌曲时获取Album_Id并进行设置,虽然存在Id(> 0),但它不会将歌曲添加到专辑中。这是我的代码:
//fetch album id
Cursor cursor = contentResolver.query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
new String[] {MediaStore.Audio.Albums._ID},
MediaStore.Audio.Albums.ALBUM + "=?" +
" and " + MediaStore.Audio.Albums.ARTIST + "=?" ,
new String[] {albumName,albumArtistName},
MediaStore.Audio.Albums.ALBUM + " ASC");
if(cursor != null && cursor.getCount() > 0){
cursor.moveToFirst();
albumId = cursor.getLong(cursor.
getColumnIndex(MediaStore.Audio.Albums._ID));
cursor.close();
}
//create new song with the album_id if one exists
ContentValues values = new ContentValues();
values.put(MediaStore.Audio.Media.TITLE, getName());
values.put(MediaStore.Audio.Media.DISPLAY_NAME, getName());
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.DATE_ADDED, System.currentTimeMillis());
values.put(MediaStore.Audio.Media.DATE_MODIFIED,System.currentTimeMillis());
values.put(MediaStore.Audio.Media.IS_MUSIC, true);
values.put(MediaStore.Audio.Media.DATA, fileName);
String artistName = getArtist_name();
if(!TextUtils.isEmpty(albumArtistName))
artistName = albumArtistName;
values.put(MediaStore.Audio.Media.ARTIST, artistName);
if(albumId > 0){
values.put(MediaStore.Audio.Media.ALBUM_ID, albumId);
}
else if(!TextUtils.isEmpty(albumName)){
values.put(MediaStore.Audio.Media.ALBUM, albumName);
}
Uri base = MediaStore.Audio.Media.getContentUriForPath(fileName);
Uri newUri = contentResolver.insert(base, values);
// Notifiy the media application on the device
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));