Konrad Konrad - 11 months ago 73
Java Question

Android Studio music player cant read from sdcard, only internal memory

I apologize if this turns out to be a stupid question, it might turn out as a quick fix but I just cant figure it out. I'm building a music player in android studio and none of the songs on the sdcard dont show up in the list view, only the ones in internal memory, even though I did implement getExternalStorageDirectory() and added the permission in the manifest file.
Any input on this or constructive criticism is greatly apreciated. Here is the main java class.

public class MainActivity extends AppCompatActivity {

ListView lv;
String[] items;

protected void onCreate(Bundle savedInstanceState) {
lv = (ListView) findViewById(R.id.lvPlayList);

//---------------------------------------------> V HERE V <-------------------------

final ArrayList<File> mySongs = findSongs(Environment.getExternalStorageDirectory());
items = new String[ mySongs.size() ];

for(int i = 0; i<mySongs.size(); i++) {
items[i] = mySongs.get(i).getName().toString().replace(".mp3", "").replace(".wav", "");

ArrayAdapter<String> adp = new ArrayAdapter<>(getApplicationContext(), R.layout.song_layout, R.id.textView, items);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startActivity(new Intent(getApplicationContext(), Player.class).putExtra("pos",position).putExtra("songlist",mySongs));

public ArrayList<File> findSongs(File root) {
ArrayList<File> al = new ArrayList<>();
File[] files = root.listFiles();
for(File singleFile : files) {
if(singleFile.isDirectory() && !singleFile.isHidden()) {
} else {
if(singleFile.getName().endsWith(".mp3") || singleFile.getName().endsWith(".wav")) {
return al;

public void toast(String text) {
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();

Answer Source

Use this method to get the list of filePath from your device

 ArrayList<String> findSongs(String rootPath) {
    ArrayList<String> fileList = new ArrayList<>();
       File rootFolder = new File(rootPath);
       File[] files = rootFolder.listFiles(); //here you will get NPE if directory doesn't contains  any file,handle it like this.
       for (File file : files) {
         if (file.isDirectory()) {
               if (findSongs(file.getAbsolutePath()) != null) {
                } else {
         } else if (file.getName().endsWith(".mp3")) {
    return fileList;
    }catch(Exception e){
       return null;

but you need to get files from sdCard, for that pass "/storage/sdcard1/" as the parameter for findSongs(String path) method like this:

onCreate(Bundle svaeInstance){
    ArrayList<String> listOfFilePath=findSongs("/storage/sdcard1/"); // here you will get all the files path which contains .mp3 at the end.
 //do the remaining stuff
  }else { Toast.makeText(this, "sdCard not available", Toast.LENGTH_SHORT).show();

Note: use "/storage/sdcard1/" for reading files from sdCard and use Environment.getExternalStorageDirectory().getAbsolutePath() for reading files from phone memory.

Hope this will help you.