如何使用MediaStore.Audio.Albums.ALBUM_ART显示专辑封面?
我试图build立一个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中的一个空白屏幕。
以下是我如何获取歌曲的专辑封面:
Cursor cursor = getActivity().managedQuery(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_ART
是MediaStore.Audio.Albums
一个属性,不能直接使用作为歌曲元数据。
如果你有相册的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,你可以使用任何图片加载器Glide , Picaso , UIL来显示图片。
**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; }
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);
你应该使用Uri.parse(“content:// media / external / audio / albumart”); 查询albumart。 第一个答案可能会在某些电话(至less是我的)
下面的代码为我工作。 我知道它已经被回答了,这可能对检查参考的人有用。
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