skip to Main Content

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);

}

}`

2

Answers


  1. Chosen as BEST ANSWER

    Wow it now work, the problem was located in Cursor audioCursor = getContentResolver().query(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, proj, null, null, null); I changed getContentResolver query to INTERNAL_CONTENT_URI coz I do not have sdcard on my device.

    Thank you all for your support.


  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search