2013-07-10 18 views
5

Próbuję zbudować odtwarzacz MP3 i chcę, aby obraz wyświetlał okładki poszczególnych utworów. Próbowałem następujące, ale to nie działa.Jak mogę wyświetlić okładkę albumu za pomocą 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); 

Kiedy próbuję odtworzyć utwory, wszystko, co dostaję, to pusty ekran w ImageView.

+0

zobaczyć również http://stackoverflow.com/a/28624058/3496570 i http://stackoverflow.com/a/28624084/3496570 – Nepster

Odpowiedz

12

Oto jak uzyskać okładkę albumu do utworu:

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 
} 

albumId dotyczy MediaStore.Audio.Media.ALBUM_ID dla tej piosenki.

Jeśli szukasz okładki albumu dla konkretnej piosenki (a nie na liście albumów), o ile wiem, jest to proces dwuetapowy, ponieważ ALBUM_ART jest własnością MediaStore.Audio.Albums i nie jest dostępny bezpośrednio jako metadane utworu.

+0

Hej Ken, w jaki sposób można wyświetlić okładkę albumu dla całego albumu ....? –

+0

Co zrobić z ciągiem po jego uzyskaniu? Jak mogę przekonwertować uri na bitmapę? –

+1

@AnkitSrivastava ciąg jest ścieżką do obrazu albumu, dzięki czemu można go przekształcić w bitmapę, na przykład za pomocą 'BitmapFactory.decodeFile (String path)' –

0

Powinieneś użyć Uri.parse ("content: // media/external/audio/albumart"); zapytać o albumart. 1st odpowiedź może dostać wyjątek na jakimś telefonie (przynajmniej moje)

4

Jeśli masz album ID masz Album uri Image: -

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

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

A jeśli masz uri obrazu można użyć dowolny z programów ładujących obrazy, aby wyświetlić obrazy.

       **OR** 

można napisać własną ładowarka obraz

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

Odpowiedź udzielona przez Kena ma również rację, ale jako nowicjusz muszę poświęcić dużo czasu, aby to zadziałało. Dlatego właśnie przesłałem pełną odpowiedź. –

0

Poniższy kod pracował dla mnie. Wiem, że odpowiedź została już udzielona, ​​może być przydatna dla kogoś, kto szuka referencji.

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); 

      } 
     } 
    }); 
} 

dla kompletnego kodu wizyty http://vasistaguru.blogspot.com/2017/02/get-albumart-and-trackname-using.html

2

Ta metoda powrotu ArrayList z ścieżki utworu i okładki albumów.

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; 
} 

to jest 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; 
} 
}