2011-07-06 3 views
4

나는 비슷한 질문과 답변을 다른 웹 사이트에서 이미 보았습니다. 또한 일부 장치 (CyanogenMod 7.1을 실행하는 G2X, 맞춤 ROM을 실행하는 아내의 HD2 및 Android 2.1을 실행하는 에뮬레이터)에서 작동하는 솔루션이 있습니다. 그러나 CyanogenMod를 실행하는 나의 구석에서 작동하지는 않습니다.Android에서 앨범 아트를 가져 오는 가장 강력한 방법

내 질문은 : 모든 안드로이드 기기에서 앨범 아트를 가져올 수있는 가장 강력하고 일반적인 방법은 무엇입니까? 특정 기기, 버전 또는 음악 응용 프로그램에 대한 문제는 무엇입니까? (타사 플레이어를 의미하지는 않습니다. Google 뮤직과 기존 뮤직 클라이언트를 의미합니까?) 나의 현재 코드는 다음과 같습니다 사전에

// 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

내가 미디어 저장소에서 앨범 아트를 반환 하나 개 기능을 첨부 할 수 있습니다. 함수에서 우리는 Media Store에서 얻은 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

this.이 답변에 감사드립니다 참조 URI 코드 (및 광산) 유효 항상 사용합니다? 심지어 sdcard가없는 장치에서도 가능합니까? – Ani

+0

여기에 sdcard가 장치에 삽입 된 경우에만 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); 

더 완전한 샘플 코드 여기에 안드로이드 음악 플레이어 소스 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); 

,