Android掃描sd卡和系統檔案

來源:互聯網
上載者:User
原創作品,轉載請註明
如果你做過多媒體應用,一定會苦惱過,怎樣擷取sd卡中的多媒體檔案。android還是很強大的,如果你知道怎麼調用android的api,萬事就ok了。
 當手機或模擬器開機時,會調用android的MediaScanner,掃描sd卡和記憶體裡的檔案。以下是log資訊。
12-13 15:39:11.062: VERBOSE/MediaPlayerService(67): Create new media retriever from pid 349
12-13 15:39:11.082: DEBUG/MediaScannerService(349): getDefaultLocale =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: 44ms
12-13 15:39:11.142: DEBUG/MediaScanner(349):     scan time: 13ms
12-13 15:39:11.142: DEBUG/MediaScanner(349): postscan time: 2ms
12-13 15:39:11.142: DEBUG/MediaScanner(349):    total time: 59ms
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
那麼掃描後的記錄它儲存到哪裡了呢。哈。你覺得在哪裡呢?data/data/com.android.media/providers/databases/external
它存了些什麼資訊呢,拉出來看看吧:
 
 那麼,我們直接使用ContentProvider就可以直接擷取到sd卡中多媒體的資訊了,你還用去listfile嗎?還用去自己解析媒體檔案中的資訊麼(時間長度,檔案名稱,專輯名。。應有盡有哦)?
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);

好了,最後一個問題
 當你往sd卡中添加一些多媒體檔案的時候,android沒有自動將它重新整理到資料庫中。那麼我們怎麼讓它手動重新整理呢,如下:
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, "SD卡中沒有歌曲,請添加後再掃描", 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;}

  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.