With this feature, I get a list of music folders on my phone.

public ArrayList<ModelFolder> scanFolders(File dir){ String d; File[] files = dir.listFiles(); for(File file:files){ if(file.isDirectory()) { scanFolders(file); } else { if(file.getAbsolutePath().endsWith(".mp3")) { int i = dir.toString().lastIndexOf("/"); d= dir.toString().substring(i+1); String count = CountInFolder(dir.toString()); if(data.size()>0){ if(data.get(data.size()-1).getTitle().equals(d)){ } else { data.add(new ModelFolder(d,"",dir.toString(),count)); } } else{ data.add(new ModelFolder(d,"",dir.toString(),"")); } } } } return data; } m.scanFolders(Environment.getExternalStorageDirectory()); 

But I need to scan and on the sd card. How to implement it?

  • And using cursor and MediaStore did not try? - Android Android
  • Can you give an example? - user186301
  • Here, for example stackoverflow.com/a/33383909/4829111 - Android Android
  • Did as there. Still does not see sd card - user186301

1 answer 1

Starting with Android 4.4 in the SDK, there is a function Context.getExternalFilesDirs(String) that returns paths to drives. Works, but not always. In my project, I get the paths to the external and internal drive in the following way:

  ArrayList<String> allPaths = new ArrayList<>(); ArrayList<String> sdPaths = new ArrayList<>(); for (File file : context.getExternalFilesDirs("external")) { if (file != null) { int index = file.getAbsolutePath().lastIndexOf("/Android/data"); if (index > 0) { String path = file.getAbsolutePath().substring(0, index); try { path = new File(path).getCanonicalPath(); } catch (Exception e) { Log.e("Exc getExtSdCardPaths", e.getCause().toString()); } allPaths.add(path); if (!file.equals(context.getExternalFilesDir("external"))) { sdPaths.add(path); } } } } 
  • And what will happen in allPaths and what in sdPaths? - user186301
  • Well, by the names of the variables: allpaths - all paths (map and internal memory) and sdpaths - paths to map - Creati8e