我正在使用android MediaStore。我可以在设备上获得音乐列表,以及与每个媒体项目相关的所有详细信息(标题,艺术家,持续时间等)
我希望能够显示相册列表及其相册艺术家。
要说清楚,每个音轨都有一个艺术家,如“NoFX”,但如果曲目出现在编辑CD上,如Punk-O-Rama,则艺术家是“NoFX”,但专辑艺术家可能会像“各位艺术家。“
我在这里回顾了这个问题:
Android - Getting Album Artist from Cursor
我正在尝试使用MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI和MediaStore.Audio.Albums.ARTIST。但是,这似乎没有返回正确的结果。
查看Android源代码,我可以看到事实上在MediaStore.Audio.Media.EXTERNAL_CONTENT_URI后面有一个album_artist字段,但它标记为“@hide”。 (见https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/provider/MediaStore.java处的行~1170)。
我发现此字段正在查看其他[相关] Android源代码: https://android.googlesource.com/platform/development/+/master/apps/Development/src/com/android/development/MediaScannerActivity.java
我写了一个简单的app / activity来测试这个:
package com.ma.albumartisttest;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.util.SparseArray;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// query all media and get the album_artist field
Cursor cursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Media.ALBUM_ID, "album_artist"}, null, null, null);
// Store an id=>name map
SparseArray<String> albumArtistNames = new SparseArray<String>();
if (cursor != null) {
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
String mediaAlbumArtist = cursor.getString(1);
if (mediaAlbumArtist != null && mediaAlbumArtist.toLowerCase().contains("various")) {
// loop through the cursor, save "album_artist"s that look like "various"
albumArtistNames.put((int) cursor.getLong(0), mediaAlbumArtist);
}
}
cursor.close();
}
String msg;
if (albumArtistNames.size() == 0) {
msg = "No 'various' artists found!";
Log.d("TESTALBUMARTIST", msg);
} else {
StringBuffer out = new StringBuffer();
for (int i = 0; i < albumArtistNames.size(); i++) {
// loop through the albums found above
int albumId = albumArtistNames.keyAt(i);
String album_artist = albumArtistNames.get(albumId);
Cursor albumCursor = getContentResolver().query(
MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ARTIST},
MediaStore.Audio.Albums._ID + "=?",
new String[]{"" + albumId}, null);
if (albumCursor != null) {
if (albumCursor.moveToFirst()) {
// print out what was found.
String artistFromAlbumsDB = albumCursor.getString(1);
Log.d("TESTALBUMARTIST", album_artist + ":" + artistFromAlbumsDB);
out.append("Album id: " + albumId).append('\n')
.append("Artist from media table: " + album_artist).append('\n')
.append("Artist from albums table: " + artistFromAlbumsDB)
.append("\n\n");
}
albumCursor.close();
}
}
msg = out.toString();
}
// show the results on-screen
TextView tv = new TextView(this);
tv.setText(msg);
setContentView(tv);
}
}
此活动产生以下logcat输出:
12-20 14:16:45.688 22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists:Mike Garson
12-20 14:16:45.708 22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists:Franco Corelli
12-20 14:16:45.728 22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists:The Foreshadowing
12-20 14:16:45.748 22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists - Polyvinyl Record Co:Volcano, I'm Still Excited!!
12-20 14:16:45.778 22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists - Curve Music:GRAND:PM
12-20 14:16:45.808 22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists - DiN:Ian Boddy
12-20 14:16:45.828 22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists:SNFU
12-20 14:16:45.858 22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists:Pulley
12-20 14:16:45.878 22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists:Osker
12-20 14:16:45.908 22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists:Pennywise
12-20 14:16:45.938 22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists:NoFX
12-20 14:16:45.958 22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists:Death By Stereo
12-20 14:16:45.978 22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists (Sillage Intemporel):Sheri Malckin
我在运行4.4.2的Nexus 7上运行此功能。我也在运行4.1.1的华为Prism II上测试并看到了相同的行为
为什么不能让专辑艺术家工作直截了当而不是“隐藏”?
我期望相册查询也会返回“各位艺术家”。
感谢所有帮助。
答案 0 :(得分:0)
我没有看到您在代码中的任何位置定义或导入ListActivity。
看看这两个例子: