Android扫描sd卡和系统文件

本文介绍了Android系统的MediaScanner工作原理及其实现方式。MediaScanner用于扫描SD卡和内存中的多媒体文件,并将其信息保存到特定数据库中。文章提供了通过ContentProvider获取多媒体文件信息的方法,并展示了如何触发手动刷新以更新数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

来源:http://www.cnblogs.com/stay/articles/1898932.html

当手机或模拟器开机时,会调用android的MediaScanner,扫描sd卡和内存里的文件。
那么扫描后的记录它保存到哪里了呢。你觉得在哪里呢?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;
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值