MediaStore
Fetch Audio/MP3 files from specific folder of device or fetch all files
First, add the following permissions to the manifest of your project in order to enable device storage access:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Then, create the file AudioModel.class and put the following model class into it in order to allow getting and setting list items:
public class AudioModel {
String aPath;
String aName;
String aAlbum;
String aArtist;
public String getaPath() {
return aPath;
}
public void setaPath(String aPath) {
this.aPath = aPath;
}
public String getaName() {
return aName;
}
public void setaName(String aName) {
this.aName = aName;
}
public String getaAlbum() {
return aAlbum;
}
public void setaAlbum(String aAlbum) {
this.aAlbum = aAlbum;
}
public String getaArtist() {
return aArtist;
}
public void setaArtist(String aArtist) {
this.aArtist = aArtist;
}
}
Next, use the following method to read all MP3 files from a folder of your device or to read all files of your device:
public List<AudioModel> getAllAudioFromDevice(final Context context) {
final List<AudioModel> tempAudioList = new ArrayList<>();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.Audio.AudioColumns.DATA, MediaStore.Audio.AudioColumns.TITLE, MediaStore.Audio.AudioColumns.ALBUM, MediaStore.Audio.ArtistColumns.ARTIST,};
Cursor c = context.getContentResolver().query(uri, projection, MediaStore.Audio.Media.DATA + " like ? ", new String[]{"%utm%"}, null);
if (c != null) {
while (c.moveToNext()) {
AudioModel audioModel = new AudioModel();
String path = c.getString(0);
String name = c.getString(1);
String album = c.getString(2);
String artist = c.getString(3);
audioModel.setaName(name);
audioModel.setaAlbum(album);
audioModel.setaArtist(artist);
audioModel.setaPath(path);
Log.e("Name :" + name, " Album :" + album);
Log.e("Path :" + path, " Artist :" + artist);
tempAudioList.add(audioModel);
}
c.close();
}
return tempAudioList;
}
The code above will return a list of all MP3 files with the music’s name, path, artist, and album. For more details please refer to the Media.Store.Audio documentation.
In order to read files of a specific folder, use the following query (you need to replace the folder name):
Cursor c = context.getContentResolver().query(uri,
projection,
MediaStore.Audio.Media.DATA + " like ? ",
new String[]{"%yourFolderName%"}, // Put your device folder / file location here.
null);
If you want to retrieve all files from your device, then use the following query:
Cursor c = context.getContentResolver().query(uri,
projection,
null,
null,
null);
Note: Don’t forget to enable storage access permissions.
Now, all you have to do is to call the method above in order to get the MP3 files:
getAllAudioFromDevice(this);
Example with Activity
public class ReadAudioFilesActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio_list);
/**
* This will return a list of all MP3 files. Use the list to display data.
*/
getAllAudioFromDevice(this);
}
// Method to read all the audio/MP3 files.
public List<AudioModel> getAllAudioFromDevice(final Context context) {
final List<AudioModel> tempAudioList = new ArrayList<>();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.Audio.AudioColumns.DATA,MediaStore.Audio.AudioColumns.TITLE ,MediaStore.Audio.AudioColumns.ALBUM, MediaStore.Audio.ArtistColumns.ARTIST,};
Cursor c = context.getContentResolver().query(uri, projection, MediaStore.Audio.Media.DATA + " like ? ", new String[]{"%utm%"}, null);
if (c != null) {
while (c.moveToNext()) {
// Create a model object.
AudioModel audioModel = new AudioModel();
String path = c.getString(0); // Retrieve path.
String name = c.getString(1); // Retrieve name.
String album = c.getString(2); // Retrieve album name.
String artist = c.getString(3); // Retrieve artist name.
// Set data to the model object.
audioModel.setaName(name);
audioModel.setaAlbum(album);
audioModel.setaArtist(artist);
audioModel.setaPath(path);
Log.e("Name :" + name, " Album :" + album);
Log.e("Path :" + path, " Artist :" + artist);
// Add the model object to the list .
tempAudioList.add(audioModel);
}
c.close();
}
// Return the list.
return tempAudioList;
}
}