Get list of all music files on Android device
To get list of all music files in Sketchware android project, follow the steps below.
1. In main.xml add a ListView listview1.
2. Add a Custom View list_item.xml. In this add an ImageView imageview1, and five TextViews textview_title, textview_artist, textview_album, textview_duration, and textview_path. Select list_item as Custom View of listview1.
3. Switch on AppCompat and Design.
4. In Image Manager, add an image ic_music_note.
5. In Permission Manager, add following permissions:
- android.permission.INTERNET
- android.permission.READ_EXTERNAL_STORAGE
- android.permission.READ_MEDIA_AUDIO
6. Add a String variable art, a Map variable map, and a ListMap musicListMap.
7. Add following imports:
import android.provider.MediaStore;
import android.net.Uri;
import android.database.Cursor;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.Manifest;
8. Create a more block readAllMusicFiles and put codes given below. After codes, use block to display musicListMap in listview1.
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] projection = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.ALBUM_ID
};
Cursor cursor = getContentResolver().query(
uri,
projection,
MediaStore.Audio.Media.IS_MUSIC + "!= 0",
null,
MediaStore.Audio.Media.TITLE + " ASC"
);
if (cursor != null) {
while (cursor.moveToNext()) {
String title = cursor.getString(1);
String artist = cursor.getString(2);
String album = cursor.getString(3);
long duration = cursor.getLong(4);
String path = cursor.getString(5);
long albumId = cursor.getLong(6);
Uri albumArtUri = Uri.parse("content://media/external/audio/albumart/" + albumId);
int totalSeconds = (int) (duration / 1000);
int minutes = totalSeconds / 60;
int seconds = totalSeconds % 60;
String formatted_duration = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
map = new HashMap<>();
map.put("title", title);
map.put("artist", artist);
map.put("album", album);
map.put("duration", formatted_duration);
map.put("path", path);
boolean hasAlbumArt = false;
try (InputStream is =
getContentResolver().openInputStream(albumArtUri)) {
if (is != null) {
hasAlbumArt = true;
}
} catch (Exception e) {
hasAlbumArt = false;
}
if (hasAlbumArt){
map.put("albumArt", albumArtUri.toString());
} else {
map.put("albumArt", "");
}
musicListMap.add(map);
}
cursor.close();
}
9. Create another more block request_permissions and put following codes in it.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (checkSelfPermission(Manifest.permission.READ_MEDIA_AUDIO)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(
new String[]{Manifest.permission.READ_MEDIA_AUDIO},
1001
);
}
} else {
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
1001
);
}
}
}
@Override
public void onRequestPermissionsResult(int _requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(_requestCode, permissions, grantResults);
if (_requestCode == 1001) {
_readAllMusicFiles();
}
10. In onCreate, use blocks request_permissions and readAllMusicFiles.
11. In onBindCustomView, use blocks as shown below.
12. Save and run the project.




Comments
Post a Comment