2011-07-06 30 views
4

請注意,我已經在這裏和其他網站上通過了類似的問題和答案。我也有一個適用於某些設備的解決方案(運行CyanogenMod 7.1的我的G2X,運行自定義ROM的妻子的HD2以及運行Android 2.1的模擬器)。它不,但是,在我的Nook上運行CyanogenMod。在Android中獲取專輯封面的最穩健的方式

我的問題是:什麼是在所有Android設備上獲取專輯封面的最強大和最通用的方法?什麼是特定設備,版本或音樂應用程序的陷阱(我不是指第三方播放器,我的意思是Google Music與舊的音樂客戶端)?我當前的代碼是:

// Is this what's making my code fail on other devices? 
public final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart"); 

// This works, and well on all devices 
private int[] getAlbumIds(ContentResolver contentResolver) 
{ 
    List<Integer> result = new ArrayList<Integer>(); 
    Cursor cursor = contentResolver.query(MediaStore.Audio.Media.getContentUri("external"), new String[]{MediaStore.Audio.Media.ALBUM_ID}, null, null, null); 

    if (cursor.moveToFirst()) 
    { 
     do{ 
      int albumId = cursor.getInt(0); 
      if (!result.contains(albumId)) 
       result.add(albumId); 
     } while (cursor.moveToNext()); 
    } 

    int[] resultArray = new int[result.size()]; 
    for (int i = 0; i < result.size(); i++) 
     resultArray[i] = result.get(i); 

    return resultArray; 
} 

// This is the bit I want to make more robust, make sure that it works on all devices 
private Shader getAlbumArt(ContentResolver contentResolver, int albumId, int width, int height) 
{ 
    Uri uri = ContentUris.withAppendedId(sArtworkUri, albumId); 
    InputStream input = null; 
    try { 
     input = contentResolver.openInputStream(uri); 
     if (input == null) 
      return null; 

     Bitmap artwork = BitmapFactory.decodeStream(input); 
     input.close(); 
     if (artwork == null) 
      return null; 

     Bitmap scaled = Bitmap.createScaledBitmap(artwork, width, height, true); 
     if (scaled == null) 
      return null; 

     if (scaled != artwork) 
      artwork.recycle(); 
     artwork = scaled; 

     return new BitmapShader(artwork, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 

由於提前, Ananth

回答

15

在這裏,我可以附加一個函數是從媒體商店返回專輯封面。在這裏,我們只需要傳遞我們從媒體商店獲得的album_id。

public Bitmap getAlbumart(Long album_id) 
    { 
     Bitmap bm = null; 
     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(); 
       bm = BitmapFactory.decodeFileDescriptor(fd); 
      } 
    } catch (Exception e) { 
    } 
    return bm; 
} 
+0

謝謝您的回答,是URI你的代碼(和我)使用總是有效?即使在沒有SD卡的設備上? – Ani

+0

這裏只有在SD卡插入設備時才需要使用uri。 –

+0

非常感謝! :D –

0

下面的代碼片段返回MediaStore中存在的專輯封面緩存的URI。可能會有所幫助。

  Cursor cursorAudio = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, {MediaStore.Audio.Media.ALBUM_ID, MediaStore.Audio.Media.DATA}, MediaStore.Audio.Media.DATA+ " LIKE \"" + path+ "\"", null, null);if(cursorAudio != null && cursorAudio.moveToFirst()){ 
     Long albumId = Long.valueOf(cursorAudio.getString(cursorAudio.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID))); 
     cursorAudio.close(); 
     Cursor cursorAlbum = managedQuery(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, {MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART}, MediaStore.Audio.Albums._ID+ "=" + albumId, null, null); 
     if(cursorAlbum != null && cursorAlbum.moveToFirst()){ 
      String uri = cursorAlbum.getString(cursorAlbum.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART)); 
      cursorAlbum.close(); 
      if(uri != null){ 
       return Uri.parse(uri); 
      } 
     } 
2
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart"); 
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id); 
ContentResolver res = context.getContentResolver(); 
InputStream in = res.openInputStream(uri); 
Bitmap artwork = BitmapFactory.decodeStream(in); 

更完整的示例代碼,這裏的Android音樂播放源https://github.com/android/platform_packages_apps_music/blob/master/src/com/android/music/MusicUtils.java方法getArtworkQuick被發現。

0

我想用畢加索圖書館修改Chirag Raval的答案。它使用簡單,功能強大。

final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart"); 
    Uri uri = ContentUris.withAppendedId(sArtworkUri, arrayList.get(i).getArt()); 
    Picasso.with(context).load(uri).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher) 
      .into(ivPic); 

的完整文檔,請參閱this.