Get cover picture by song - android

Is it possible to get a cover picture by song and not by album.
Because I have one self combined album with songs and they all have different cover pictures.
But when I want to query them I always get the same picture returned.
String[] ARG_STRING = {MediaStore.Audio.Media.ALBUM_ID};
...
String albumCover = _cursor.getString(_cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
...
MusicUtils.getArtwork(this, -1, Integer.parseInt(albumID));
So i would like to know how it's possible to get an cover image of an song.
I know MusicUtils supports getArtwork by SongId, but what ID should I use because MediaStore.Audio.Media._ID is not working.

I'm not familiar with MusicUtils, however, you should be able to get the cover art from the file itself by using MediaMetadataRetriever. Here is a brief code snippet showing how to use it. The uri referenced is the content uri for the file you want to retrieve the art for.
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
byte[] rawArt;
Bitmap art;
BitmapFactory.Options bfo=new BitmapFactory.Options();
mmr.setDataSource(getApplicationContext(), uri);
rawArt = mmr.getEmbeddedPicture();
// if rawArt is null then no cover art is embedded in the file or is not
// recognized as such.
if (null != rawArt)
art = BitmapFactory.decodeByteArray(rawArt, 0, rawArt.length, bfo);
// Code that uses the cover art retrieved below.

Related

How to make a VideoView Preview UI in Android?

So I'm a beginner in android and I've been trying to experiment media related elements of Android. So far, I was able to display video, but I saw Viber and some other similar apps have some slick way of displaying video like below, wherein when tapped will open an app viewer wherein images/videos of app can be seen there:
Here is my code, it starts video immediately... I tried the .seekTo(100), ..seekTo(0) option but both gives me a black screen.
Uri uri = Uri.parse(mVideoFileName);
mVideoMessageView.setVideoURI(uri);
mediaC.setAnchorView(mVideoMessageView);
mVideoMessageView.start();
I wonder, how they made an overlay of a play button on the video, duration, and even timestamp and have a snapshot of it. Any advice would greatly be appreciated.
Thanks a lot.
int id = **"The Video's ID"**
ImageView iv = (ImageView ) convertView.findViewById(R.id.imagePreview);
ContentResolver crThumb = getContentResolver();
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 1;
Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, id, MediaStore.Video.Thumbnails.MICRO_KIND, options);
iv.setImageBitmap(curThumb);
Also you can use Glide for this
Glide
.with( context )
.load( Uri.fromFile( new File( filePath ) ) )
.into( imageViewGifAsBitmap );

Querying mp3 cover image using glide

I've been developing an android music player and I would like to load the cover image which each mp3 tag has. I previously used the following code to extract the image but, when the list view is starts scrolling it gives a lot of errors and the app crashes at random times. The code is as following.
MediaMetadataRetriever retrieve = new MediaMetadataRetriever();
retrieve.setDataSource(songsList.get(songIndex).get("songPath"));
byte [] data = mmr.getEmbeddedPicture();
This throws an error saying the following message and at random points the application crashes.
getEmbeddedPicture: Call to getEmbeddedPicture failed
So I used the glide library to load the images using the following code.
Glide.with(parent.getContext()).load(currSong.getPath())
.placeholder(R.drawable.default_img).into(holder.albumImage);
The thing is I pass the song path to the load function but doesn't load any image to it! I logged the path and it shows the correct path to the mp3 file.
How can I load the images using the Glide library! If there is a tutorial on this, please be kind enough to let me know.
I did the following to retrieve and set the image in the list.
To retrieve album art for the song:-
This is the query
final Cursor mCursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ALBUM_ID}, null, null,
"LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");
To capture URI
long album_id=mCursor.getLong(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, album_id);
And then to set in list via Adapter
Uri uri=Uri.parse(data.get(position).getAlbum_art());
try {
bitmap = MediaStore.Images.Media.getBitmap(ctx.getContentResolver(), uri);
holder.image.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}

Album art not displayed in music player in android

I'm building a music player in Android. I'm displaying the album art for currently playing song. This is my code to retrieve the embedded image:-
public Bitmap getAlbumArt(String filePath) {
Bitmap bm = null;
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(filePath);
byte [] data = mmr.getEmbeddedPicture();
// convert the byte array to a bitmap
if(data != null) {
bm = BitmapFactory.decodeByteArray(data, 0, data.length);
}
return bm;
}
This code works fine and is able to retrieve album art but only for some songs. However, I can see the album art for all the songs in thumbnail view in Windows.
Could somebody explain what might be the reason behind this? Also, if there is an alternate way to achieve the same?
Thank You!

Extract album cover from mp3 file in android

Hello Everyone ,
In my media player i need to display the album cover(i dont know how it pronounced actually..I hope right) of the song. I knew for that i have to extract the image from the song itself but how? m wondering. So any help, if possible with some sorts of code. Thanks.
for api 10 and above
android.media.MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(songsList.get(songIndex).get("songPath"));
byte [] data = mmr.getEmbeddedPicture();
//coverart is an Imageview object
// convert the byte array to a bitmap
if(data != null)
{
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
coverart.setImageBitmap(bitmap); //associated cover art in bitmap
}
else
{
coverart.setImageResource(R.drawable.fallback_cover); //any default cover resourse folder
}
coverart.setAdjustViewBounds(true);
coverart.setLayoutParams(new LinearLayout.LayoutParams(500, 500));
Try FFmpegMediaMetadataRetriever:
FFmpegMediaMetadataRetriever retriever = new FFmpegMediaMetadataRetriever();
retriever.setDataSource(uri);
byte [] data = retriever.getEmbeddedPicture();
// convert the byte array to a bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
// do something with the image ...
// mImageView.setImageBitmap(bitmap);
retriever.release();
You can try with Picasso by using album_id. it is open source & less memory cache.
Dependency:
implementation 'com.squareup.picasso:picasso:2.71828'
Code:
String albumId = songObject.getAlbum_id();
final Uri albumUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(albumUri, Long.parseLong(albumId));
Picasso.get().load(uri)
.fit()
.centerCrop()
.error(R.drawable.img_album)
.into(holder.imgAlbumSongObject);
This is very late. but, may help someone.
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(filePath);
String albumName = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM));
Note this will work only at api level 10 or above

cover art on android

I am developing a sort of media player for android. The question is how can i get the cover art of audio file on android.
For example the default android media player shows album covers when listing albums, how can i get this artworks.
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
ContentResolver res = context.getContentResolver();
InputStream in = res.openInputStream(uri);
Bitmap artwork = BitmapFactory.decodeStream(in);
More complete sample code can be found in Android Music player source here https://github.com/android/platform_packages_apps_music/blob/master/src/com/android/music/MusicUtils.java method getArtworkQuick.
You can use
MediaMetadataRetriever
class and get track info i.e. Title,Artist,Album,Image
Bitmap GetImage(String filepath) //filepath is path of music file
{
Bitmap image;
MediaMetadataRetriever mData=new MediaMetadataRetriever();
mData.setDataSource(filePath);
try{
byte art[]=mData.getEmbeddedPicture();
image=BitmapFactory.decodeByteArray(art, 0, art.length);
}
catch(Exception e)
{
image=null;
}
return image;
}
I don't know why everybody is making it so complicated you can use Glide to achieve this in simplest and efficient way with just 1 line of code
Declare this path in App Constants -
final public static Uri sArtworkUri = Uri
.parse("content://media/external/audio/albumart");
Get Image Uri using Album ID
Uri uri = ContentUris.withAppendedId(PlayerConstants.sArtworkUri,
listOfAlbums.get(position).getAlbumID());
Now simply display album art using uri :-
Glide.with(context).load(uri).placeholder(R.drawable.art_default).error(R.drawable.art_default)
.crossFade().centerCrop().into(holder.albumImage);
Glide will handle caching, scaling and lazy loading of images for you.
Hope it help somebody.
Here i can attach one function that is return album art from media store .
Here in function we just have to pass the album_id which we get from Media store .
public Bitmap getAlbumart(Long album_id)
{
Bitmap bm = null;
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);
}
} catch (Exception e) {
}
return bm;
}
Based on your comments to others, it seems like your question is less about Android and more about how to get album art in general. Perhaps this article on retrieving album art from Amazon will be helpful. Once you have a local copy and store it as Nick has suggested, I believe you should be able to retrieve it the way Fudgey suggested.
Android only recognizes files named "AlbumArt.jpg" as Album Covers. Just put the pictures with that name in the album folder and you'll be fine..
I don't know if you read that google is making Stackoverflow the official Android app development Q&A medium but for beginner questions... Now, I know nothing about developing in andriod, but a quick search of the Android Developers site, I found this:
MediaStore.Audio.AlbumColumns
Hopefully it'll help.

Categories

Resources