【问题标题】:How can I display Album Art using MediaStore.Audio.Albums.ALBUM_ART?如何使用 MediaStore.Audio.Albums.ALBUM_ART 显示专辑封面?
【发布时间】:2013-07-08 13:54:25
【问题描述】:

我正在尝试构建一个 MP3 播放器,我希望 ImageView 显示相应歌曲的专辑封面。我尝试了以下方法,但它不起作用。

albumcover = (ImageView) findViewById(R.id.cover);

String coverPath = songsList.get(songIndex).get(MediaStore.Audio.Albums.ALBUM_ART);
Drawable img = Drawable.createFromPath(coverPath);
albumcover.setImageDrawable(img);

当我尝试播放歌曲时,我得到的只是 ImageView 中的一个空白屏幕。

【问题讨论】:

标签: android albumart


【解决方案1】:

以下是我获取歌曲专辑封面的方法:

Cursor cursor = getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, 
                new String[] {MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART}, 
                MediaStore.Audio.Albums._ID+ "=?", 
                new String[] {String.valueOf(albumId)}, 
                null);

if (cursor.moveToFirst()) {
    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
    // do whatever you need to do
}

albumId 指的是那首歌曲的MediaStore.Audio.Media.ALBUM_ID

如果您正在寻找特定歌曲的专辑封面(而不是在专辑列表中),据我所知,这是一个两阶段的过程,因为 ALBUM_ARTMediaStore.Audio.Albums 的属性并且是不能直接作为歌曲元数据使用。

【讨论】:

  • @AnkitSrivastava 该字符串是专辑封面图像的路径,因此您可以将其转换为位图,例如使用BitmapFactory.decodeFile(String path)
  • @PrashantPatel 分两个阶段我的意思是,据我所知,您无法直接从歌曲中获取专辑封面,这是专辑的属性。所以首先你必须得到这首歌所属的专辑,然后你才能从中得到艺术。
  • @KenWolf 是的,你是对的,我设法通过 Metadataretriver 做到了这一点,但我想知道是否有其他方法。感谢您的回复。
  • @KenWolf 为时已晚,但有办法this 但它很热​​吗?
  • 此方法可能会在 Android 10 上导致问题,因为路径不可访问(EACCESS 错误)。授予权限(External_Storage 的读写)是不够的。
【解决方案2】:

你应该使用 Uri.parse("content://media/external/audio/albumart");查询专辑封面。第一个答案可能会在某些电话上出现异常(至少是我的)

【讨论】:

    【解决方案3】:

    如果您有专辑 ID,您将获得专辑图片 uri:-

    final public static Uri sArtworkUri = Uri
                .parse("content://media/external/audio/albumart");      
    
    Uri uri = ContentUris.withAppendedId(PlayerConstants.sArtworkUri,
                    listOfAlbums.get(position).getAlbumID());
    

    如果你有一个图像 uri,你可以使用任何图像加载器 GlidePicasoUIL 来显示图像。

                                   **OR**
    

    您可以编写自己的图像加载器

    public Bitmap getAlbumart(Context context, Long album_id) {
        Bitmap albumArtBitMap = 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();
                albumArtBitMap = BitmapFactory.decodeFileDescriptor(fd, null,
                        options);
                pfd = null;
                fd = null;
            }
        } catch (Error ee) {
        } catch (Exception e) {
        }
    
        if (null != albumArtBitMap) {
            return albumArtBitMap;
        }
        return getDefaultAlbumArtEfficiently(context.getResources());
    }
    
    
    
    public Bitmap getDefaultAlbumArtEfficiently(Resources resource) {
    
        if (defaultBitmapArt == null) {
    
            defaultBitmapArt = decodeSampledBitmapFromResource(resource,
                    R.drawable.default_album_art, UtilFunctions
                            .getUtilFunctions().dpToPixels(85, resource),
                    UtilFunctions.getUtilFunctions().dpToPixels(85, resource));
    
        }
        return defaultBitmapArt;
    }
    

    【讨论】:

    • 别忘了调用 fd.close() (BitmapFactory.decodeFileDescriptor 也不这样做)
    【解决方案4】:
     ContentResolver musicResolve = getContentResolver();
        Uri smusicUri = android.provider.MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
        Cursor music =musicResolve.query(smusicUri,null         //should use where clause(_ID==albumid)
            ,null, null, null);
    
    
    
        music.moveToFirst();            //i put only one song in my external storage to keep things simple
        int x=music.getColumnIndex(android.provider.MediaStore.Audio.Albums.ALBUM_ART);
            String thisArt = music.getString(x);
    
    
         Bitmap bm= BitmapFactory.decodeFile(thisArt);
            ImageView image=(ImageView)findViewById(R.id.image);
            image.setImageBitmap(bm);
    

    【讨论】:

    • Ken 给出的答案也是正确的,但作为一个新手,我必须花很多时间才能让它发挥作用。这就是我上传完整答案的原因。
    【解决方案5】:

    下面的代码对我有用。我知道它已经回答了,它可能对检查参考的人有用。

     public void getAlbumArt() {
        try {
            ContentResolver cr = getContentResolver();
            Uri uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
    
            Cursor cursor = cr.query(uri, null, null, null, null);
    
            if (cursor != null && cursor.moveToFirst()) {
                int albumart = cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART);
                do {
                    String Albumids = cursor.getString(albumart);
                    Albumid.add(Albumids);
    
                } while (cursor.moveToNext());
            }cursor.close();
        } catch (NumberFormatException e){
            e.printStackTrace();
        }
    }
    public void getSelectedPath(){
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String path= String.valueOf(listView.getItemAtPosition(i));
    
                    if(path.equals("null")){
                    ImageView imgv=(ImageView)view.findViewById(R.id.imageView);
                    imgv.setImageResource(R.drawable.unknowalbum);
                    imgv.setMaxHeight(50);
                    imgv.setMaxWidth(50);
    
                }
                else{
                    Drawable image=Drawable.createFromPath(path);
                    ImageView imgview=(ImageView)view.findViewById(R.id.imageView);
                    imgview.setImageDrawable(image);
    
                }
            }
        });
    }
    

    完整代码请访问http://vasistaguru.blogspot.com/2017/02/get-albumart-and-trackname-using.html

    【讨论】:

      【解决方案6】:

      此方法返回带有歌曲路径和专辑封面的 ArrayList。

      public static ArrayList<CommonModel> getAllMusicPathList(Context context) {
          ArrayList<CommonModel> musicPathArrList = new ArrayList<>();
          Uri songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
      
          Cursor cursorAudio = context.getContentResolver().query(songUri, null, null, null, null);
          if (cursorAudio != null && cursorAudio.moveToFirst()) {
      
              Cursor cursorAlbum;
              if (cursorAudio != null && cursorAudio.moveToFirst()) {
      
                  do {
                      Long albumId = Long.valueOf(cursorAudio.getString(cursorAudio.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)));
                      cursorAlbum = context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                              new String[]{MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART},
                              MediaStore.Audio.Albums._ID + "=" + albumId, null, null);
      
                          if(cursorAlbum != null && cursorAlbum.moveToFirst()){
                              String albumCoverPath = cursorAlbum.getString(cursorAlbum.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
                              String data = cursorAudio.getString(cursorAudio.getColumnIndex(MediaStore.Audio.Media.DATA));
                              musicPathArrList.add(new CommonModel(data,albumCoverPath ,false));
                          }
      
                       } while (cursorAudio.moveToNext());
              }
          }
          return musicPathArrList;
      }
      

      这是 CommonModel 。

      public class CommonModel {
      
      private String path;
      private boolean selected;
      
      public String getAlbumCoverPath() {
          return albumCoverPath;
      }
      
      public void setAlbumCoverPath(String albumCoverPath) {
          this.albumCoverPath = albumCoverPath;
      }
      
      private String albumCoverPath;
      
      public CommonModel(String path, String albumCoverPath, boolean b) {
          this.path = path;
          this.albumCoverPath=albumCoverPath;
          this.selected=b;
      }
      
      public String getPath() {
          return path;
      }
      
      public void setPath(String path) {
          this.path = path;
      }
      
      public boolean getSelected() {
          return selected;
      }
      
      public void setSelected(boolean selected) {
          selected = selected;
      }
      }
      

      【讨论】:

        【解决方案7】:

        示例代码

        public static Uri getAlbumArtUri(long albumId) {
            return ContentUris.withAppendedId(Uri.parse("content://media/external/audio/albumart"), albumId);
        }
        
        
        ArrayList<Uri> albumArtUris = new ArrayList<>();
        Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[]{"album_id"}, null, null, null);
        cursor.moveToFirst();
        do {
            long ablumId = cursor.getLong(cursor.getColumnIndexOrThrow("album_id"));
            albumArtUris.add(getAlbumArtUri(ablumId));
        } while (cursor.moveToNext());
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多