When the Android system is started, it will automatically detect the SD card files and collect music files to generate a database file. We only need to access the information in the database table to obtain the required File Code as follows:
<Span style = "font-size: 14px; white-space: pre;"> </span> <span style = "font-size: 14px; "> // obtain all the songs. query (MediaStore. audio. media. EXTERNAL_CONTENT_URI, null, MediaStore. audio. media. DEFAULT_SORT_ORDER); </span>
Obtain the cursor object to access the media resources in the cursor.
If (cursor. moveToFirst () {do {String title = cursor. getString (cursor. getColumnIndex (MediaStore. audio. media. TITLE); String singer = cursor. getString (cursor. getColumnIndex (MediaStore. audio. media. ARTIST); String album = cursor. getString (cursor. getColumnIndex (MediaStore. audio. media. ALBUM); long size = cursor. getLong (cursor. getColumnIndex (MediaStore. audio. media. SIZE); long time = cursor. getLo Ng (cursor. getColumnIndex (MediaStore. audio. media. DURATION); String url = cursor. getString (cursor. getColumnIndex (MediaStore. audio. media. DATA); int _ id = cursor. getInt (cursor. getColumnIndex (MediaStore. audio. media. _ ID); String name = cursor. getString (cursor. getColumnIndex (MediaStore. audio. media. DISPLAY_NAME); String sbr = name. substring (name. length ()-3, name. length ();} while (cursor. moveTo Next (); <p> </p> <span style = "font-size: 14px; color: # 3366ff; background-color: rgb (255,255,255) "> however, if your mobile phone downloads a new song file after it is turned on, it cannot be accessed through the above method because it has not been added to the data table, in this way, we need to rewrite the file search method. </Span> </p> <br> </p> <pre name = "code" class = "java"> // obtain all files in this path public static List <String> getAllFile (String path) {List <String> fileList = new ArrayList <String> (); File folder = new File (path); addFile (fileList, folder); return fileList ;} public static void addFile (List <String> fileList, File f) {if (f. isDirectory () {File [] files = f. listFiles (); for (File f1: files) {addFile (fileList, f1) ;}} else {String filename = f. getName (); if (filename. length ()> 4 & filename. substring (filename. length ()-4 ). equalsIgnoreCase (". mp3 ") {fileList. add (f. getAbsolutePath () ;}}</pre> <span style = "font-size: 14px "> <br> </span> you only need to call <pre name =" code "class =" java "> getAllFile (Environment. getExternalStorageDirectory (). getAbsolutePath (); </pre> <br> <pre> </pre> <p> </p> <pre> </pre>