广播接收机代码:
public class ScanSdReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (Intent.ACTION_MEDIA_SCANNER_STARTED.equals(action))
{
//开始扫描,把你的代码放这里
}
else if (Intent.ACTION_MEDIA_SCANNER_FINISHED.equals(action))
{
//扫描结束,把你的代码放这里
}
}
}
注册广播接收机,监听SDcard扫描事件
IntentFilter intentfilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_STARTED);
intentfilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
intentfilter.addDataScheme("file");
ScanSdReceiver scanSdReceiver = new ScanSdReceiver();
registerReceiver(scanSdReceiver, intentfilter);
扫描SDCard
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath())));
================================================================================
如果你做过多媒体应用,一定会苦恼过,怎样获取sd卡中的多媒体文件。android还是很强大的,如果你知道怎么调用android的api,万事就ok了。
|
1
|
当手机或模拟器开机时,会调用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
|
|
1
|
那么,我们直接使用ContentProvider就可以直接获取到sd卡中多媒体的信息了,你还用去listfile么?还用去自己解析媒体文件中的信息么(时长,文件名,专辑名。。应有尽有哦)?
|
1
|
Cursor cursor = context.getContentResolver().query(
new
String[] { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.TITLE,
"_size>?"
,
new
String[]{
1024
*
1024
+
""
},
null
);
|
1
|
|
1
|
好了,最后一个问题
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
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
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
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
;
}
|