如果你做过多媒体应用,一定会苦恼过,怎样获取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<BR>
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<BR>
12-13 15:39:11.142: DEBUG/MediaScanner(349): prescan time: 44ms<BR>
12-13 15:39:11.142: DEBUG/MediaScanner(349): scan time: 13ms<BR>
12-13 15:39:11.142: DEBUG/MediaScanner(349): postscan time: 2ms<BR>
12-13 15:39:11.142: DEBUG/MediaScanner(349): total time: 59ms<BR>
12-13 15:39:11.152: DEBUG/MediaProvider(349): un-lock thumbn
ail 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
那么扫描后的记录它保存到哪里了呢。哈。你觉得在哪里呢?data/data/com.android.media/providers/databases/external
它存了些什么信息呢,拉出来看看吧:
那么,我们直接使用ContentProvider就可以直接获取到sd卡中多媒体的信息了,你还用去listfile么?还用去自己解析媒体文件中的信息么(时长,文件名,专辑名。。应有尽有哦)?
代码如下:
Cursor cursor = context.getContentResolver().query(<BR> MediaStore.Aud
io.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);
好了,最后一个问题<BR> 当你往sd卡中添加一些多媒体文件的时候,android没有自动将它刷新到
数据库中。那么我们怎么让它手动刷新呢,如下:
代码如下:
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_STARTED);
intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
intentFilter.a
DDDataScheme("file");
scanReceiver = new ScanSdFilesReceiver();
re
GISterReceiver(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.i
sshowing()) {
scanAlertDialog.dismiss();
}
unregisterReceiver(scanReceiver);
break;
case DISMISS:
Log.i(TAG, "dismiss");
if (scanAlertDialog!=null && scanAlertDialog.isShowing()) {
scanAlertDialog.dismiss();
}
default:
break;
}
您可能感兴趣的文章:Android SD卡上文件操作及记录
日志操作实例分析将文件放到Android模拟器的SD卡中的两种解决方法Android获取assets文件夹中的数据并写入SD卡示例android通过配置文件设置应用安装到SD卡上的方法Android
编程之在SD卡上进行文件读写操作实例详解最简单的SD卡文件遍历Android程序Android 搜索SD卡文件的
开发示例Android实现读取SD卡下所有TXT文件名并用listView显示出来的方法android 手机SD卡读写操作(以txt文本为例)实现步骤android读写sd卡操作写入数据读取数据示例Android开发之SD卡文件操作分析
0