Retrieving all the Audio files from MediaStore in Android
Posted By : Deepanshu Harbola | 01-Oct-2015
In this blog post we will use MediaStore.Audio class to reterive our audio files from android device.
MediaStore.Audio is works like a container. It contains all the audio files from internal and external storage.
We will follow some basic steps to retrieve audio files-
- Get all the audio file from MediaStore.
- Add all the retrieved files in a List.
- Display the List.
Now its time to convert these steps into android code.
1- Get Audio Files-
We will use MediaStore.Audio file to retrieve the audio files from storage(internal or external).
First we have to spacify what are the contents we are going to retrieve, for that we will create an array of projections we need.
Here is the code flick for that-
String[] proj = { MediaStore.Audio.Media._ID,MediaStore.Audio.Media.DISPLAY_NAME };
Ok, we know what content we need, now set the projection and make a query for data, for that purpose we will use Cursor interface.
Cursor is very useful when you are retrieving some data from storage. It provides read-write access tothe result set return by a query.
Here is the code flick for that-
Cursor audioCursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
2- Add result-set into a List-
We have all the data access using above query . Now add that data into a list.
Here is the code flick for that-
if(audioCursor != null){ if(audioCursor.moveToFirst()){ do{ int audioIndex = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME); audioList.add(audioCursor.getString(audioIndex)); }while(audioCursor.moveToNext()); } } audioCursor.close();
3- Display List-
Now its the time to display all the data into a View. For that we have to create a ListView in the layout and then create an Adapter to bridg between View and program part.
Here is the code flick for that-
ListView audioView = (ListView) findViewById(R.id.songView); ArrayList<string> audioList = new ArrayList<>(); ArrayAdapter<string> adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,android.R.id.text1, audioList); audioView.setAdapter(adapter);
Here is the Complete code-
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView audioView = (ListView) findViewById(R.id.songView); ArrayList<string> audioList = new ArrayList<>(); String[] proj = { MediaStore.Audio.Media._ID,MediaStore.Audio.Media.DISPLAY_NAME };// Can include more data for more details and check it. Cursor audioCursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null, null); if(audioCursor != null){ if(audioCursor.moveToFirst()){ do{ int audioIndex = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME); audioList.add(audioCursor.getString(audioIndex)); }while(audioCursor.moveToNext()); } } audioCursor.close(); ArrayAdapter<string> adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,android.R.id.text1, audioList); audioView.setAdapter(adapter); } }
Hope this post will help you..!!
Thanks.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Deepanshu Harbola
Deepanshu is an Android application developer with experience in Titanium Framework . Deepanshu likes to play basketball ,volleyball and listens to music in his free time.