'How to show thumbnail of audio files from storage in android
I want to show the audio file thumbnail with rest of information, how can i parse the thumbnail of all the audio files
Function for loading audio files in recycler view
private void loadAudio() {
ContentResolver contentResolver = getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
Cursor cursor = contentResolver.query(uri, null, selection, null, sortOrder);
if (cursor != null && cursor.getCount() > 0) {
audioList = new ArrayList<>();
while (cursor.moveToNext()) {
String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String albumId = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
audioList.add(new AudioClass(data, title, album, artist, null));
}
} else {
Log.e(TAG, "ContentResolver got null----------");
}
cursor.close();
}
**Audio File Class for showing the files in recyclerView **
public class AudioClass {
String data;
String title;
String album;
String artist;
Bitmap albumArtPath;
public AudioClass(String data, String title, String album, String artist, Bitmap albumArtPath) {
this.data = data;
this.title = title;
this.album = album;
this.artist = artist;
this.albumArtPath = albumArtPath;
}
public String getData() {
return data;
}
public String getTitle() {
return title;
}
public String getAlbum() {
return album;
}
public String getArtist() {
return artist;
}
public Bitmap getAlbumArtPath() {
return albumArtPath;
} }
The list of audio files is inflated but i cannot get the thumbnail for the audio files.
Solution 1:[1]
You can use MediaMetadataRetriever.
MediaMetadataRetriever class provides a unified interface for retrieving frame and meta data from an input media file.
For example, the following code to get a thumbnail is an audio file.
public Bitmap loadThumbnail(String absolutePath) {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(absolutePath);
byte[] data = retriever.getEmbeddedPicture();
retriever.release();
if (data == null) {
return null;
}
return BitmapFactory.decodeByteArray(data, 0, data.length);
}
The following code is to get the name of the artist.
public String getArtist(String absolutePath) {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(absolutePath);
String artist = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
retriever.release();
return artist;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mahdi Khosravi |