我知道之前已被问过但我找不到解决方案了。 我试图将一首歌的类型存储在设备的存储空间中,并且我的应用程序下面的代码崩溃了
public void getMusic() {
ContentResolver contentResolver = getContentResolver();
Uri songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor songCursor = contentResolver.query(songUri, null, null, null, null);
if (songCursor != null && songCursor.moveToFirst()) {
int songTitle = songCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int songArtist = songCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
int songAlbum = songCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM);
int songData = songCursor.getColumnIndex(MediaStore.Audio.Media.DATA);
int songID = songCursor.getColumnIndex(MediaStore.Audio.Media._ID);
MediaMetadataRetriever mr = new MediaMetadataRetriever();
Uri trackUri = ContentUris.withAppendedId(
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,songID);
mr.setDataSource(getBaseContext(), trackUri);
do {
String currentID = songCursor.getString(songID);
String currentTitle = songCursor.getString(songTitle);
String currentArtist = songCursor.getString(songArtist);
String currentAlbum = songCursor.getString(songAlbum);
String currentData = songCursor.getString(songData);
String currentGenre = mr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE);
songs.add(new Song(currentID, currentTitle, currentArtist, currentAlbum, currentGenre));
} while (songCursor.moveToNext());
}
}
答案 0 :(得分:1)
我就是这样做的: 首先得到genre_id:
public String getGenreId(Context context, String genrename) {
ContentResolver cr = context.getContentResolver();
Uri uri = MediaStore.Audio.Genres.getContentUri("external");
String genre_id = null;
String genre = MediaStore.Audio.Genres.NAME;
String id = MediaStore.Audio.Genres._ID;
String[] strcriteria = {genrename};
final String[] columns = {id, genre};
Cursor cursor = null;
try {
cursor = cr.query(uri, columns, "name=?", strcriteria, null);
} catch (Exception e) {
e.printStackTrace();
}
if (cursor != null && cursor.moveToFirst()) {
int genre_idCol = cursor
.getColumnIndex(MediaStore.Audio.Genres._ID);
genre_id = cursor.getString(genre_idCol);
cursor.close();
}
return genre_id;
}
public Cursor getgenre_TrackCursor(Context context, long genreID) {
final String track_id = MediaStore.Audio.Genres.Members._ID;
final String track_no = MediaStore.Audio.Genres.Members.TRACK;
final String track_name = MediaStore.Audio.Genres.Members.TITLE;
final String artist = MediaStore.Audio.Genres.Members.ARTIST;
final String duration = MediaStore.Audio.Genres.Members.DURATION;
final String album = MediaStore.Audio.Genres.Members.ALBUM;
final String composer = MediaStore.Audio.Genres.Members.COMPOSER;
final String year = MediaStore.Audio.Genres.Members.YEAR;
final String path = MediaStore.Audio.Genres.Members.DATA;
final ContentResolver resolve = context.getContentResolver();
Uri uri = MediaStore.Audio.Genres.Members.getContentUri("external",
genreID);
final String[] columns = {track_id, track_no, artist, track_name,
album, duration, year};
return resolve.query(uri, columns, null, null, null);
}