如何在android中显示歌曲的缩略图?

时间:2013-01-03 10:20:26

标签: android android-mediaplayer

我正在显示SDCard中的所有歌曲,包括TITLE,ARTIST,ALBUM等所有信息。

但是我对歌曲缩略图的问题。如何获得歌曲缩略图。?

以下是我的代码。

public class AndroidListMediaActivity extends ListActivity {

 SimpleCursorAdapter adapter;
 MediaPlayer myMediaPlayer;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      String[] from = {
        MediaStore.MediaColumns.TITLE};
      int[] to = {
        android.R.id.text1};

      Cursor cursor = managedQuery(
        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
        null,
        null,
        null,
        MediaStore.Audio.Media.TITLE);

      adapter = new SimpleCursorAdapter(this,
        android.R.layout.simple_list_item_1, cursor, from, to);
      setListAdapter(adapter);
  }

 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
  Cursor cursor = adapter.getCursor();
  cursor.moveToPosition(position);

  String _id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
  String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
  String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
  String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
  int duration = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));

  Uri playableUri
   = Uri.withAppendedPath(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, _id);
  myMediaPlayer = MediaPlayer.create(getBaseContext(), playableUri);
  String msgMediaPlayer;
  if(myMediaPlayer != null){
   myMediaPlayer.start();
   msgMediaPlayer = "Playing: " + playableUri + "\n";
  }else{
   msgMediaPlayer = "Cannot play: " + playableUri + "\n";
  }

  String info = msgMediaPlayer + "\n"
     + "_ID: " + _id + "\n"
     + "TITLE: " + title + "\n"
     + "ARTIST: " + artist + "\n"
     + "ALBUM: " + album + "\n"
     + "DURATION: " + duration/1000 + "s";

  Toast.makeText(this, info, Toast.LENGTH_LONG).show();
 }

}

1 个答案:

答案 0 :(得分:0)

使用以下示例对我有用。

public class Main extends ListActivity
{

String[] videoFileList = {
        "/sdcard/Video/1.mp4",
        "/sdcard/Video/2.mp4",
        "/sdcard/Video/3.mp4",
        "/sdcard/Video/5.mp4"
};

public class MyThumbnaildapter extends ArrayAdapter<String>
{

    public MyThumbnaildapter(Context context, int textViewResourceId,String[] objects) 
    {
        super(context, textViewResourceId, objects);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        // TODO Auto-generated method stub

        View row = convertView;
        if(row==null)
        {
            LayoutInflater inflater=getLayoutInflater();
            row=inflater.inflate(R.layout.second_list_item, parent, false);
        }

        TextView textfilePath = (TextView)row.findViewById(R.id.FilePath);
        textfilePath.setText(videoFileList[position]);
        ImageView imageThumbnail = (ImageView)row.findViewById(R.id.Thumbnail);

        Bitmap bmThumbnail;
        if(new File(videoFileList[position]).exists())
        {
            bmThumbnail = ThumbnailUtils.createVideoThumbnail(videoFileList[position], MediaStore.Images.Thumbnails.MINI_KIND);
            bmThumbnail=Bitmap.createScaledBitmap(bmThumbnail,150,150, true);
            imageThumbnail.setImageBitmap(bmThumbnail);
            textfilePath.setText(videoFileList[position]);
        }

        return row;
    }

}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setListAdapter(new MyThumbnaildapter(Main.this, R.layout.second_list_item, videoFileList));
}

}