Original Work, reprinted please note
If you have used multimedia applications, you will be troubled by how to obtain multimedia files in the SD card. Android is still very powerful. If you know how to call the android API, everything will be okay.
When a mobile phone or simulator is turned on, Android mediascanner is called to scan SD cards and files in memory. The following is the log information.
12-13 15:39:11. 062: verbose/mediaplayerservice (67): Create New Media retriever from PID 349
15:39:11. 082: Debug/mediascannerservice (349): getdefalocallocale = zh_cn
12-13 15:39:11. 122: Debug/surfaceflinger (102): layer: requestbuffer (this = 0x7c8c68), Index = 1, pid = 12866, W = 309, H = 192 success
12-13 15:39:11. 142: INFO/mediascanner (349): moriginalcount = 14, prune thumb flag = false
12-13 15:39:11. 142: Debug/mediascanner (349): prescan time: 44 ms
12-13 15:39:11. 142: Debug/mediascanner (349): scan time: 13 Ms
12-13 15:39:11. 142: Debug/mediascanner (349): postscan time: 2 ms
12-13 15:39:11. 142: Debug/mediascanner (349): Total time: 59 Ms
12-13 15:39:11. 152: Debug/mediaprovider (349): un-lock thumbnail worker
12-13 15:39:11. 152: Debug/mediaprovider (349): un-lock thumbnail worker
12-13 15:39:11. 182: Debug/mediascannerservice (349): done scanning volume external
So where is the scanned record saved. Haha. Where do you think it is? Data/data/COM. Android. Media/providers/databases/external
What information does it store? Let's take a look:
Then, we can directly use contentprovider to obtain the multimedia information in the SD card. Do you still use listfile? Do you still need to parse the information in the media file by yourself (duration, file name, album name .. Everything )?
Cursor cursor = context. getcontentresolver (). Query (
Mediastore. Audio. Media. external_content_uri,
New String [] {mediastore. Audio. Media. _ id, mediastore. Audio. Media. display_name, mediastore. Audio. Media. title,
Mediastore. Audio. Media. Duration, mediastore. Audio. Media. Artist, mediastore. Audio. Media. album,
Mediastore. Audio. Media. Year, mediastore. Audio. Media. mime_type, mediastore. Audio. Media. Size, mediastore. Audio. Media. Data}
, "_ Size>? ", New string [] {1024*1024 +" "}, null );
Okay, the last question
When you add some multimedia files to the SD card, Android does not automatically refresh them to the database. So how can we manually refresh it, as follows:
Intentfilter = new intentfilter (intent. action_media_scanner_started); intentfilter. addaction (intent. action_media_scanner_finished); intentfilter. adddatascheme ("file"); scanreceiver = new scansdfilesreceiver (); registerreceiver (scanreceiver, intentfilter); sendbroadcast (new intent (intent. action_media_mounted, Uri. parse ("file: //" + environment. getexternalstoragedirectory (); private class SC Ansdfilesreceiver extends broadcastreceiver {public void onreceive (context, intent) {string action = intent. getaction (); If (intent. action_media_scanner_started.equals (Action) {scanhandler. sendemptymessage (started);} If (intent. action_media_scanner_finished.equals (Action) {scanhandler. sendemptymessage (finished) ;}} private handler scanhandler = new handler () {public void handlemessag E (Message MSG) {super. handlemessage (MSG); Switch (MSG. what) {case started: mydialog scandialog = new mydialog (locallist. this); scanalertdialog = scandialog. scanfile (); scanalertdialog. show (); log. I (TAG, "showing"); break; case finished: arraylist <song> tempsongs = readfilelist. readdatafromsd (locallist. this, local); If (tempsongs! = NULL & tempsongs. Size ()> 0) {If (songs! = NULL & songs. size ()> 0) {songs. clear (); songs. addall (tempsongs); songadapter. notifydatasetchanged ();} else {songs = new arraylist <song> (); songs. addall (tempsongs); initsong_lv () ;}} else {toast. maketext (locallist. this, "There are no songs in the SD card, please add them and then scan", toast. length_short ). show ();} log. I (TAG, "Finish"); If (scanalertdialog! = NULL & scanalertdialog. isshowing () {scanalertdialog. dismiss ();} unregisterreceiver (scanreceiver); break; Case dismiss: log. I (TAG, "Dismiss"); If (scanalertdialog! = NULL & scanalertdialog. isshowing () {scanalertdialog. Dismiss ();} default: break ;}