【问题标题】:how to get pic for each song? i come to know that its known as album art . i get song name , artist, album from my code but not album art [duplicate]如何获取每首歌的图片?我知道它被称为专辑封面。我从我的代码中获取歌曲名称、艺术家、专辑,但没有专辑封面 [重复]
【发布时间】:2017-02-02 14:42:48
【问题描述】:
 String[] STAR = {"*"};

    Cursor cursor;
    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";


    cursor = getContentResolver().query(uri, STAR, selection, null, null);

    if (cursor != null) {

        if (cursor.moveToFirst()) {
            int i = 0;
            Drawable img;
            do {
                String songName = cursor
                        .getString(cursor
                                .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));

                path[i] = cursor.getString(cursor
                        .getColumnIndex(MediaStore.Audio.Media.DATA));

                String albumName = cursor.getString(cursor
                        .getColumnIndex(MediaStore.Audio.Media.ALBUM));
                albumId = cursor
                        .getString(cursor
                                .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
                song.add(songName);

//上面的代码可以完美运行,但是下一行会产生错误
字符串路径 = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART)); System.out.println(路径); 我++; } while (cursor.moveToNext());

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以使用以下方法获取歌曲的专辑封面:

    public static Bitmap getAlbumart(Context context, Long album_id){
        Bitmap bm = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        try{
              final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
              Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
              ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
              if (pfd != null){
                  FileDescriptor fd = pfd.getFileDescriptor();
                  bm = BitmapFactory.decodeFileDescriptor(fd, null, options);
                  pfd = null;
                  fd = null;
              }
          } catch(Error ee){}
          catch (Exception e) {}
          return bm;
        }
    

    【讨论】:

    • 如何在自定义通知和列表中将此位图对象设置为图像
    • 代码是完美的,任何人都想知道如何使用上面的代码请评论
    • @HarshRaval - 没有收到您的第一个查询,您能详细说明一下吗?
    • 我通过你的代码解决了它。冷静一下,你的答案是完美的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多