android 扫描SD卡与系统文件

  当手机或模拟器开机时,会调用android的MediaScanner,扫描sd卡和内存里的文件。以下是log信息。
 
1 12-13 15:39:11.062: VERBOSE/MediaPlayerService(67): Create new media retriever from pid 349
 
1 12-13 15:39:11.082: DEBUG/MediaScannerService(349): getDefaultLocale =zh_CN
 
1 12-13 15:39:11.122: DEBUG/SurfaceFlinger(102): Layer::requestBuffer(this=0x7c8c68), index=1, pid=12866, w=309, h=192 success
 
1 12-13 15:39:11.142: INFO/MediaScanner(349): mOriginalCount = 14, prune thumb flag = false
 
1 12-13 15:39:11.142: DEBUG/MediaScanner(349):  prescan time: 44ms
 
1 12-13 15:39:11.142: DEBUG/MediaScanner(349):     scan time: 13ms
 
1 12-13 15:39:11.142: DEBUG/MediaScanner(349): postscan time: 2ms
 
1 12-13 15:39:11.142: DEBUG/MediaScanner(349):    total time: 59ms
 
1 12-13 15:39:11.152: DEBUG/MediaProvider(349): un-lock thumbnail worker
 
1 12-13 15:39:11.152: DEBUG/MediaProvider(349): un-lock thumbnail worker
 
1 12-13 15:39:11.182: DEBUG/MediaScannerService(349): done scanning volume external
 
1 那么扫描后的记录它保存到哪里了呢。哈。你觉得在哪里呢?data/data/com.android.media/providers/databases/external
 
1 它存了些什么信息呢,拉出来看看吧:
 
1  那么,我们直接使用ContentProvider就可以直接获取到sd卡中多媒体的信息了,你还用去listfile么?还用去自己解析媒体文件中的信息么(时长,文件名,专辑名。。应有尽有哦)?
 
1 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);
 
1 好了,最后一个问题
 当你往sd卡中添加一些多媒体文件的时候,android没有自动将它刷新到数据库中。那么我们怎么让它手动刷新呢,如下:
 
01 IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_STARTED);
02         intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
03         intentFilter.addDataScheme("file");
04         scanReceiver = new ScanSdFilesReceiver();
05         registerReceiver(scanReceiver, intentFilter);
06         sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
07   
08   
09 private class ScanSdFilesReceiver extends BroadcastReceiver {
10         public void onReceive(Context context, Intent intent) {
11             String action = intent.getAction();
12             if (Intent.ACTION_MEDIA_SCANNER_STARTED.equals(action)) {
13                 scanHandler.sendEmptyMessage(STARTED);
14             }
15             if (Intent.ACTION_MEDIA_SCANNER_FINISHED.equals(action)) {
16                 scanHandler.sendEmptyMessage(FINISHED);
17             }
18         }
19     }
20   
21   
22 private Handler scanHandler = new Handler() {
23         public void handleMessage(Message msg) {
24             super.handleMessage(msg);
25             switch (msg.what) {
26             case STARTED:
27                 MyDialog scanDialog = new MyDialog(LocalList.this);
28                 scanAlertDialog = scanDialog.scanFile();
29                 scanAlertDialog.show();
30                 Log.i(TAG, "showing");
31                 break;
32             case FINISHED:
33                 ArrayList tempSongs = ReadFileList.readDataFromSD(LocalList.this, LOCAL);
34                 if (tempSongs != null && tempSongs.size()>0) {
35                     if (songs != null && songs.size()>0) {
36                         songs.clear();
37                         songs.addAll(tempSongs);
38                         songAdapter.notifyDataSetChanged();
39                     }else {
40                         songs = new ArrayList();
41                         songs.addAll(tempSongs);
42                         initSong_lv();
43                     }
44                 }else {
45                     Toast.makeText(LocalList.this, "SD卡中没有歌曲,请添加后再扫描", Toast.LENGTH_SHORT).show();
46                 }
47                 Log.i(TAG, "finish");
48                 if (scanAlertDialog!=null && scanAlertDialog.isShowing()) {
49                     scanAlertDialog.dismiss();
50                 }
51                 unregisterReceiver(scanReceiver);
52                 break;
53             case DISMISS:
54                 Log.i(TAG, "dismiss");
55                 if (scanAlertDialog!=null && scanAlertDialog.isShowing()) {
56                     scanAlertDialog.dismiss();
57                 }
58             default:
59                 break;
60             }

你可能感兴趣的:(android 扫描SD卡与系统文件)