How does the android music player search for music files on the SD card?

Source: Internet
Author: User

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>

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.