android在啟動的時候會啟動MediaScannerService掃描系統上的多媒體檔案,然後將這些多媒體檔案的資訊加入到多媒體資料庫中,應用程式要取得這些多媒體資訊就是從這個多媒體資料庫裡面去取的,並不是從SD卡中取。也就是說,如果開機後增加或刪除了一些多媒體,這個多媒體資料庫是不會自動重新整理的。android提供了兩個Intent來發廣播讓系統自動重新整理多媒體資料庫,分別是Intent.ACTION_MEDIA_MOUNTED和Intent.Action_MEDIA_SCANNER_SCAN_FILE,前面的是掃描整個SD卡,後面的針對某個檔案進行掃描,發了Intent.ACTION_MEDIA_MOUNTED這個廣播後,還可以通過廣播接收器監聽ACTION_MEDIA_SCAN_STARTED和ACTION_MEDAI_SCAN_FINISH這兩個廣播,分別是開始掃描和掃描完畢時系統發出的。進行全卡掃描的話需要3-5秒的時間(我的情況),針對某個檔案掃描的沒有試過,呵呵,懶了。最近DLNA的DMC需要用到重新整理媒體庫的功能,不然之前一直是要是添加了新的檔案,就重啟手機,暈死了。網上搜尋了一趟,很多都是建議:
[java]
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath())));
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath())));
可是我試了之後發現,每當我增加或減少了多媒體檔案後,我的整個音樂列表都不見了,不知道是哪裡出了原因。後來參考了以下兩位的做法,終於把功能實現了:
我的具體實現是:
[java]
public class MainActivity extends Activity {
private MediaScannerConnection mediaScanConn = null;
private MusicSannerClient client = null;
private File filePath = null;
private String fileType = null;
@SuppressLint("SdCardPath")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
client = new MusicSannerClient();
mediaScanConn = new MediaScannerConnection(this, client);
scanfile(new File("/sdcard"));
}
class MusicSannerClient implements
MediaScannerConnection.MediaScannerConnectionClient {
public void onMediaScannerConnected() {
Log.e("---------", "media service connected");
if (filePath != null) {
if (filePath.isDirectory()) {
File[] files = filePath.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory())
scanfile(files[i]);
else {
mediaScanConn.scanFile(
files[i].getAbsolutePath(), fileType);
}
}
}
}
}
filePath = null;
fileType = null;
}
public void onScanCompleted(String path, Uri uri) {
// TODO Auto-generated method stub
mediaScanConn.disconnect();
}
}
private void scanfile(File f) {
this.filePath = f;
mediaScanConn.connect();
}
}
public class MainActivity extends Activity {
private MediaScannerConnection mediaScanConn = null;
private MusicSannerClient client = null;
private File filePath = null;
private String fileType = null;
@SuppressLint("SdCardPath")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
client = new MusicSannerClient();
mediaScanConn = new MediaScannerConnection(this, client);
scanfile(new File("/sdcard"));
}
class MusicSannerClient implements
MediaScannerConnection.MediaScannerConnectionClient {
public void onMediaScannerConnected() {
Log.e("---------", "media service connected");
if (filePath != null) {
if (filePath.isDirectory()) {
File[] files = filePath.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory())
scanfile(files[i]);
else {
mediaScanConn.scanFile(
files[i].getAbsolutePath(), fileType);
}
}
}
}
}
filePath = null;
fileType = null;
}
public void onScanCompleted(String path, Uri uri) {
// TODO Auto-generated method stub
mediaScanConn.disconnect();
}
}
private void scanfile(File f) {
this.filePath = f;
mediaScanConn.connect();
}
}