I'm developing a very little audio player. The problem is: after many issues I managed to build a ListView with the songs retrieved from any folder in the external storage and list them with title, artist and album name. Now I want to add in the ListView also the cover album. The cover must be taken from the image embedded in the audio file of the song.
I tried to use MediaMetadataRetriever but I can't get the complete Uri for each file, so I can't set the data source for it. How can I get the cover? If I had the byte array I'd use a BitmapFactory... but I haven't.
BTW, this is my code... In the activity, this is the void that search for audio files in the external storage and put them in a list:
public void retrieveAudioFiles(){
songsList = new SongsList();
Uri sd = Audio.Media.EXTERNAL_CONTENT_URI;
String[] cols = {Audio.Media.TITLE,Audio.Media.ARTIST,Audio.Media.ALBUM};
String where = Audio.Media.IS_MUSIC;
Cursor audioCursor = getContentResolver().query(sd,cols,where,null,null);
while (audioCursor.moveToNext()){
int posColTitle = audioCursor.getColumnIndex(Audio.Media.TITLE);
int posColArtist = audioCursor.getColumnIndex(Audio.Media.ARTIST);
int posColAlbum = audioCursor.getColumnIndex(Audio.Media.ALBUM);
String songTitle = audioCursor.getString(posColTitle);
String songArtist = audioCursor.getString(posColArtist);
String songAlbum = audioCursor.getString(posColAlbum);
songsList.add(new Song(songTitle,songArtist,songAlbum));
}
audioCursor.close();
}
This is the adapter:
public class SongsAdapter extends BaseAdapter {
private SongsList songsList;
private LayoutInflater songInf;
public SongsAdapter(Context c, SongsList theSongs){
super();
songsList=theSongs;
songInf=LayoutInflater.from(c);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return songsList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return songsList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
RowWrapper wrapper;
if (convertView == null)
{
convertView = songInf.inflate(
R.layout.song_row, null);
wrapper = new RowWrapper(convertView);
convertView.setTag(wrapper);
}
else
{
wrapper = (RowWrapper) convertView.getTag();
}
Song song = (Song) getItem(position);
wrapper.populate(song);
return convertView;
}
private static class RowWrapper
{
private TextView titleTextView;
private TextView artistTextView;
private TextView albumTextView;
//private ImageView coverImageView;
public RowWrapper(View convertView)
{
titleTextView = (TextView) convertView.findViewById(R.id.textTitle);
artistTextView = (TextView) convertView.findViewById(R.id.textArtist);
albumTextView = (TextView) convertView.findViewById(R.id.textAlbum);
//coverImageView = (ImageView) convertView.findViewById(R.id.smallCover);
}
public void populate(Song song)
{
titleTextView.setText(song.title);
artistTextView.setText(song.artist);
albumTextView.setText(song.album);
//if (song.cover != null)
//coverImageView.setImageBitmap(song.cover);
}
}
}
This is the Song class:
public class Song {
public String title="";
public String artist="";
public String album="";
public Bitmap cover=null;
public Song(String t, String ar, String al){
title=t;
artist=ar;
album=al;
//cover=c;
}
public Song(){
}
}
This is the SongsList class (a simple ArrayList of Songs):
public class SongsList extends ArrayList<Song> {
public SongsList(){
super();
}
}
This is the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.audioplayer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.audioplayer.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
This is the layout for the single row of the ListActivity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:onClick="songPicked" >
<ImageView
android:id="@+id/smallCover"
android:layout_width="60dp"
android:layout_height="60dp"
android:contentDescription="@string/coverDescription"
android:src="@drawable/no_cover" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/labelTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/labelTitle" />
<TextView
android:id="@+id/textTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="@string/textTitle" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/labelArtist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:text="@string/labelArtist" />
<TextView
android:id="@+id/textArtist"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="@string/textArtist" />
<TextView
android:id="@+id/labelAlbum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="@string/labelAlbum" />
<TextView
android:id="@+id/textAlbum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="@string/textAlbum" />
</LinearLayout>
</LinearLayout>
Hope you can help me...
I made it by my own...
I added a long parameter for id and a Uri parameter for the path in the Song class.
In the activity, just before songsList.add(new Song(......)) (I added the Bitmap parameter in the constructor), I added these instructions:
int posColId = audioCursor.getColumnIndex(Audio.Media._ID);
long songId = audioCursor.getLong(posColId);
Uri songUri = ContentUris.withAppendedId(Audio.Media.EXTERNAL_CONTENT_URI,songId);
String[] dataColumn = {Audio.Media.DATA};
Cursor coverCursor = getContentResolver().query(songUri, dataColumn, null, null, null);
coverCursor.moveToFirst();
int dataIndex = coverCursor.getColumnIndex(Audio.Media.DATA);
String filePath = coverCursor.getString(dataIndex);
coverCursor.close();
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(filePath);
byte[] coverBytes = retriever.getEmbeddedPicture();
Bitmap songCover;
if (coverBytes!=null) //se l'array di byte non è vuoto, crea una bitmap
songCover = BitmapFactory.decodeByteArray(coverBytes, 0, coverBytes.length);
else
songCover=null;
Then I uncommented the instructions in Song class and in the Adapter.