I am trying to fetch the album art of all the songs present on the phone. I am using MediaStore to fetch all the songs title,artist etc. How should I fetch album art ? I tried using MediaMetaDataRetriever but getting confused how to use it for multiple files. Can anyone please tweak this code?

我正在尝试获取手机上所有歌曲的专辑封面。我正在使用MediaStore来获取所有歌曲标题,艺术家等。我该如何获取专辑封面?我尝试使用MediaMetaDataRetriever,但对如何将它用于多个文件感到困惑。有人可以调整这段代码吗?

Activity class:

活动类:

public void getSongList() {
    // retrieve song info
    ContentResolver musicResolver = getContentResolver();
    Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Cursor musicCursor = musicResolver.query(musicUri, null, null, null,
            null);
    metaRetriver.setDataSource(MainActivity.this,musicUri); // now how to loop over this

    if (musicCursor != null && musicCursor.moveToFirst()) {
        // get columns
        int titleColumn = musicCursor.getColumnIndex(MediaColumns.TITLE);
        int idColumn = musicCursor.getColumnIndex(BaseColumns._ID);
        int artistColumn = musicCursor.getColumnIndex(AudioColumns.ARTIST);

        // add songs to list
        do {
            long thisId = musicCursor.getLong(idColumn);
            String thisTitle = musicCursor.getString(titleColumn);
            String thisArtist = musicCursor.getString(artistColumn);
            songList.add(new Song(thisId, thisTitle, thisArtist));
        } while (musicCursor.moveToNext());

    }

3 个解决方案

#1


3

Once you have the album id, which you can get from that same cursor, you can query a different URI for the cover art path. see below for an example of approximately how I do it:

获得可从同一光标获取的相册ID后,您可以查询封面艺术路径的不同URI。请参阅下面的示例,了解我的工作方式:

private static String getCoverArtPath(Context context, long androidAlbumId) {
    String path = null;
    Cursor c = context.getContentResolver().query(
            MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
            new String[]{MediaStore.Audio.Albums.ALBUM_ART},
            MediaStore.Audio.Albums._ID + "=?",
            new String[]{Long.toString(androidAlbumId)},
            null);
    if (c != null) {
        if (c.moveToFirst()) {
            path = c.getString(0);
        }
        c.close();
    }
    return path;
}

You could get a Map of all album art by id using something like this (untested)

您可以使用以下内容获取所有专辑封面的地图(未经测试)

private static Map<Long, String> getCoverArtPaths(Context context) {
    String HashMap<Long, String> map = new HashMap<Long, String>();
    Cursor c = context.getContentResolver().query(
            MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
            new String[]{MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART},
            null,
            null,
            null);
    if (c != null) {
        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
            map.add(c.getLong(0), c.getString(1));
        }
        c.close();
    }
    // returns a mapping of Album ID => art file path
    return map;
}

更多相关文章

  1. 封面流:做无限循环(像启动器,当到达最后一项时可以回到第一项)

随机推荐

  1. 用最快的速度在 PHP 语言下实现验证码功
  2. php文件包含漏洞原理浅探
  3. php中对static关键字的理解
  4. 用laravel+Swoole实现websocket主动消息
  5. php laravel请求处理管道(装饰者模式)
  6. PHP字符逃逸导致的对象注入详解
  7. PHP操作Redis数据库常用方法
  8. PHP如何基于redis的分布式锁防止高并发重
  9. 专注于处理 PHP 跨域的 CORS 中间件 1.4.
  10. php中字符“\n”与“<br />”的区别