Home > OS >  How to get all audio files from device with MediaStore
How to get all audio files from device with MediaStore

Time:01-21

I am trying to get all the media files from the device Internal Storage. I used MediaStore to retrieve it but it return only one song.

Please I want it to return all song in internal Storage, but it return only one song, please help me to check where the problem is.

Here is my code main activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.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);

}

}`

CodePudding user response:

You can try it like this.

  • 1st add these two permission manifests and give programmatically

     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
  • For Android 13 or higher you need only this one permission for audio

    <uses-permission android:name="android.permission.READ_MEDIA_AUDIO"/>
    
  • And Get All Audios Like this

         ArrayList<String> audioLists = new ArrayList<>();
    
         String[] strings = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME};// Can include more data for more details and check it.
    
         Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, strings, null, null, null);
    
         if (cursor != null) {
             if (cursor.moveToFirst()) {
                 do {
                     int audioIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
    
                     audioLists.add(cursor.getString(audioIndex));
                 } while (cursor.moveToNext());
             }
         }
         cursor.close();
    
     for (int i = 0 ; i < audioLists.size(); i   ){
         Log.e("audioListss" ,"Audio Path ---->>>  "   audioLists.get(i));
     }
    
  • I get All Audios See below

enter image description here

CodePudding user response:

It still not working, it also return a track, not all track.

Here is the screenshot

  • Related