我正在开发一个非常小的音频播放器。问题是:经过很多问题,我设法用从外部存储中任何文件夹中检索到的歌曲构建了一个ListView,并列出了它们的标题,艺术家和专辑名称。现在我想在ListView中添加封面专辑。封面必须取自歌曲音频文件中嵌入的图像。
我尝试使用MediaMetadataRetriever,但无法获取每个文件的完整Uri,因此无法为其设置数据源。我如何获得保险?如果我有字节数组,我会使用BitmapFactory ...,但是没有。
顺便说一句,这是我的代码...
在活动中,这是在外部存储中搜索音频文件并将其放在列表中的空白:
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();
}
这是适配器:
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);
}
}
}
这是Song类:
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(){
}
}
这是SongsList类(一个简单的SongArrayList):
public class SongsList extends ArrayList<Song> {
public SongsList(){
super();
}
}
这是清单:
<?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>
这是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>
希望你能帮我...
最佳答案
我自己做的...
我在Song类中为id添加了一个long参数,为路径添加了一个Uri参数。
在活动中,就在songsList.add(new Song(......))之前(我在构造函数中添加了Bitmap参数),我添加了以下指令:
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;
然后我取消注释Song类和适配器中的说明。