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.
Copy codeThe Code is as follows: 12-13 15:39:11. 062: VERBOSE/MediaPlayerService (67): Create new media retriever from pid 349 <BR>
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 <BR>
12-13 15:39:11. 142: DEBUG/MediaScanner (349): prescan time: 44 ms <BR>
12-13 15:39:11. 142: DEBUG/MediaScanner (349): scan time: 13 ms <BR>
12-13 15:39:11. 142: DEBUG/MediaScanner (349): postscan time: 2 ms <BR>
12-13 15:39:11. 142: DEBUG/MediaScanner (349): total time: 59 ms <BR>
12-13 15:39:11. 152: DEBUG/MediaProvider (349): un-lock thumbnail worker <BR>
12-13 15:39:11. 152: DEBUG/MediaProvider (349): un-lock thumbnail worker <BR>
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/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 )?Copy codeThe Code is as follows: Cursor cursor = context. getContentResolver (). query (<BR> MediaStore. audio. media. EXTERNAL_CONTENT_URI, <BR> new String [] {MediaStore. audio. media. _ ID, MediaStore. audio. media. DISPLAY_NAME, MediaStore. audio. media. TITLE, <BR> MediaStore. audio. media. DURATION, MediaStore. audio. media. ARTIST, MediaStore. audio. media. ALBUM, <BR> MediaStore. audio. media. YEAR, MediaStore. audio. media. MIME_TYPE, MediaStore. audio . Media. SIZE, MediaStore. Audio. Media. DATA} <BR>, "_ size>? ", New String [] {1024*1024 +" "}, null );
Okay, the last question <BR> when you add some multimedia files to the SD card, android does not automatically refresh the files to the database. So how can we manually refresh it, as follows:Copy codeThe Code is as follows: IntentFilter 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 ScanSdFilesReceiver extends BroadcastReceiver {
Public void onReceive (Context context, Intent 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 handleMessage (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 scan again", 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;
}