2013-07-10 42 views
5

我試圖建立一個MP3播放器,我希望ImageView能夠顯示各首歌曲的專輯封面。我試過以下,但它不起作用。如何使用MediaStore.Audio.Albums.ALBUM_ART顯示專輯封面?

albumcover = (ImageView) findViewById(R.id.cover); 

String coverPath = songsList.get(songIndex).get(MediaStore.Audio.Albums.ALBUM_ART); 
Drawable img = Drawable.createFromPath(coverPath); 
albumcover.setImageDrawable(img); 

當我嘗試播放歌曲時,我所得到的只是ImageView中的一個空白屏幕。

+0

也看到http://stackoverflow.com/a/28624058/3496570和http://stackoverflow.com/a/28624084/3496570 – Nepster

回答

12

以下是我得到的專輯封面一首歌:

Cursor cursor = getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, 
       new String[] {MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART}, 
       MediaStore.Audio.Albums._ID+ "=?", 
       new String[] {String.valueOf(albumId)}, 
       null); 

if (cursor.moveToFirst()) { 
    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART)); 
    // do whatever you need to do 
} 

albumIdMediaStore.Audio.Media.ALBUM_ID爲這首歌。

如果您正在尋找特定歌曲(而不是專輯列表)的專輯封面,據我所知這是一個兩階段過程,因爲ALBUM_ARTMediaStore.Audio.Albums的一個屬性,不能直接使用作爲歌曲元數據。

+0

嘿肯,我怎麼能顯示專輯封面整張專輯....? –

+0

如何得到它後的字符串呢?我如何將uri轉換成位圖? –

+1

@AnkitSrivastava該字符串是專輯藝術圖像的路徑,所以你可以把它變成一個位圖,例如使用'BitmapFactory.decodeFile(String path)' –

0

你應該使用Uri.parse(「content:// media/external/audio/albumart」);查詢albumart。第一答案可能會得到一些手機上的異常(至少我的)

4

如果你有相冊ID你相冊圖片URI: -

final public static Uri sArtworkUri = Uri 
      .parse("content://media/external/audio/albumart");  

Uri uri = ContentUris.withAppendedId(PlayerConstants.sArtworkUri, 
       listOfAlbums.get(position).getAlbumID()); 

如果你有一個形象的URI,您可以使用圖像加載器Glide,Picaso,UIL中的任何一個來顯示圖像。

       **OR** 

你可以寫你自己的圖像加載器

public Bitmap getAlbumart(Context context, Long album_id) { 
    Bitmap albumArtBitMap = null; 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    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(); 
      albumArtBitMap = BitmapFactory.decodeFileDescriptor(fd, null, 
        options); 
      pfd = null; 
      fd = null; 
     } 
    } catch (Error ee) { 
    } catch (Exception e) { 
    } 

    if (null != albumArtBitMap) { 
     return albumArtBitMap; 
    } 
    return getDefaultAlbumArtEfficiently(context.getResources()); 
} 



public Bitmap getDefaultAlbumArtEfficiently(Resources resource) { 

    if (defaultBitmapArt == null) { 

     defaultBitmapArt = decodeSampledBitmapFromResource(resource, 
       R.drawable.default_album_art, UtilFunctions 
         .getUtilFunctions().dpToPixels(85, resource), 
       UtilFunctions.getUtilFunctions().dpToPixels(85, resource)); 

    } 
    return defaultBitmapArt; 
} 
5
ContentResolver musicResolve = getContentResolver(); 
    Uri smusicUri = android.provider.MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI; 
    Cursor music =musicResolve.query(smusicUri,null   //should use where clause(_ID==albumid) 
     ,null, null, null); 



    music.moveToFirst();   //i put only one song in my external storage to keep things simple 
    int x=music.getColumnIndex(android.provider.MediaStore.Audio.Albums.ALBUM_ART); 
     String thisArt = music.getString(x); 


    Bitmap bm= BitmapFactory.decodeFile(thisArt); 
     ImageView image=(ImageView)findViewById(R.id.image); 
     image.setImageBitmap(bm); 
+0

Ken給出的答案也是對的,但作爲一個新手,我必須花費大量時間才能使其工作。這就是爲什麼我上傳了完整答案。 –

0

下面的代碼爲我工作。我知道它已經得到了回答,這對檢查參考的人有用。

public void getAlbumArt() { 
    try { 
     ContentResolver cr = getContentResolver(); 
     Uri uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI; 

     Cursor cursor = cr.query(uri, null, null, null, null); 

     if (cursor != null && cursor.moveToFirst()) { 
      int albumart = cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART); 
      do { 
       String Albumids = cursor.getString(albumart); 
       Albumid.add(Albumids); 

      } while (cursor.moveToNext()); 
     }cursor.close(); 
    } catch (NumberFormatException e){ 
     e.printStackTrace(); 
    } 
} 
public void getSelectedPath(){ 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
      String path= String.valueOf(listView.getItemAtPosition(i)); 

       if(path.equals("null")){ 
       ImageView imgv=(ImageView)view.findViewById(R.id.imageView); 
       imgv.setImageResource(R.drawable.unknowalbum); 
       imgv.setMaxHeight(50); 
       imgv.setMaxWidth(50); 

      } 
      else{ 
       Drawable image=Drawable.createFromPath(path); 
       ImageView imgview=(ImageView)view.findViewById(R.id.imageView); 
       imgview.setImageDrawable(image); 

      } 
     } 
    }); 
} 

的完整代碼訪問http://vasistaguru.blogspot.com/2017/02/get-albumart-and-trackname-using.html

2

該方法返回的ArrayList與歌路和專輯封面。

public static ArrayList<CommonModel> getAllMusicPathList(Context context) { 
    ArrayList<CommonModel> musicPathArrList = new ArrayList<>(); 
    Uri songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 

    Cursor cursorAudio = context.getContentResolver().query(songUri, null, null, null, null); 
    if (cursorAudio != null && cursorAudio.moveToFirst()) { 

     Cursor cursorAlbum; 
     if (cursorAudio != null && cursorAudio.moveToFirst()) { 

      do { 
       Long albumId = Long.valueOf(cursorAudio.getString(cursorAudio.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID))); 
       cursorAlbum = context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, 
         new String[]{MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART}, 
         MediaStore.Audio.Albums._ID + "=" + albumId, null, null); 

        if(cursorAlbum != null && cursorAlbum.moveToFirst()){ 
         String albumCoverPath = cursorAlbum.getString(cursorAlbum.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART)); 
         String data = cursorAudio.getString(cursorAudio.getColumnIndex(MediaStore.Audio.Media.DATA)); 
         musicPathArrList.add(new CommonModel(data,albumCoverPath ,false)); 
        } 

       } while (cursorAudio.moveToNext()); 
     } 
    } 
    return musicPathArrList; 
} 

這是CommonModel。

public class CommonModel { 

private String path; 
private boolean selected; 

public String getAlbumCoverPath() { 
    return albumCoverPath; 
} 

public void setAlbumCoverPath(String albumCoverPath) { 
    this.albumCoverPath = albumCoverPath; 
} 

private String albumCoverPath; 

public CommonModel(String path, String albumCoverPath, boolean b) { 
    this.path = path; 
    this.albumCoverPath=albumCoverPath; 
    this.selected=b; 
} 

public String getPath() { 
    return path; 
} 

public void setPath(String path) { 
    this.path = path; 
} 

public boolean getSelected() { 
    return selected; 
} 

public void setSelected(boolean selected) { 
    selected = selected; 
} 
} 
相關問題