基于Android扫描sd卡与系统文件的介绍
程序员文章站
2023-11-29 22:18:16
如果你做过多媒体应用,一定会苦恼过,怎样获取sd卡中的多媒体文件。android还是很强大的,如果你知道怎么调用android的api,万事就ok了。 当手机或模拟器开机时...
如果你做过多媒体应用,一定会苦恼过,怎样获取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 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
那么扫描后的记录它保存到哪里了呢。哈。你觉得在哪里呢?data/data/com.android.media/providers/databases/external
它存了些什么信息呢,拉出来看看吧:
那么,我们直接使用contentprovider就可以直接获取到sd卡中多媒体的信息了,你还用去listfile么?还用去自己解析媒体文件中的信息么(时长,文件名,专辑名。。应有尽有哦)?
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);
好了,最后一个问题<br> 当你往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;
}
当手机或模拟器开机时,会调用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 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
那么扫描后的记录它保存到哪里了呢。哈。你觉得在哪里呢?data/data/com.android.media/providers/databases/external
它存了些什么信息呢,拉出来看看吧:
那么,我们直接使用contentprovider就可以直接获取到sd卡中多媒体的信息了,你还用去listfile么?还用去自己解析媒体文件中的信息么(时长,文件名,专辑名。。应有尽有哦)?
复制代码 代码如下:
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);
好了,最后一个问题<br> 当你往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;
}