我怎样才能从MediaStore获得专辑封面?

时间:2010-02-16 07:58:16

标签: android

如何从MediaStore获取专辑封面?我试着像:

String[] projectionImages  = new  String[]{MediaStore.Audio.Albums.ALBUM_ART, MediaStore.Audio.Albums.ALBUM_KEY};


Cursor c = contentResolver.query(MediaStore.Audio.Albums.INTERNAL_CONTENT_URI, projectionImages, null, null, null);

if(c != null){
      c.moveToFirst();
      Log.e(TAG, "am gasit " + c.getString(0) + " " + c.getString(1));
  }
 else{
      Log.e(TAG, "No image");
 }

但是没有工作..我为每个条目只检索null!

谢谢!

2 个答案:

答案 0 :(得分:1)

是否会迭代您在目录中看到的条目数量,或者数字不匹配?

以下内容适用于在MediaStore中搜索图像

if (cursor.moveToFirst()) {
do {
    int col = cursor.getColumnIndex(Images.Media.DESCRIPTION);
    String description = cursor.getString(col);
    if (new Long(item).toString().equals(description)) {
        imageId = cursor.getPosition();
        int dataColumn = cursor.getColumnIndex(Images.Media.DATA);
        filePath = cursor.getString(dataColumn);
        return true;
    }
} while (cursor.moveToNext());

答案 1 :(得分:1)

试试这个

Cursor cursorAlbum = managedQuery(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                    new String[]{MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART},MediaStore.Audio.Albums._ID+ "=" + album_id, null, null);

    if(cursorAlbum != null  && cursorAlbum.moveToFirst())
      {

        String uri = cursorAlbum.getString(cursorAlbum.getColumnIndex("album_art"));
        cursorAlbum.close();
        if(uri != null )    image.setImageURI(Uri.parse(uri));

      }