Android4.4之前可以通過發一個系統廣播來更新媒體庫,但是4.4以後就不行了,這個廣播僅限於系統發,看到網上都說可以用
MediaScannerConnection.scanFile()這個方法,但是我發現只是在刪除文件後才會更新,如果增加文件並不會更新,請問怎麼解決,我只有5個幣了,望大家幫幫忙
public class MediaScannerWrapper implements
MediaScannerConnection.MediaScannerConnectionClient {
private MediaScannerConnection mConnection;
private String mPath;
private String mMimeType;
// filePath - where to scan;
// mime type of media to scan i.e. "image/jpeg".
// use "*/*" for any media
public MediaScannerWrapper(Context ctx, String mime) {
mMimeType = mime;
mConnection = new MediaScannerConnection(ctx, this);
}
public void scan(String path) {
mPath = path;
if (mConnection.isConnected()) {
mConnection.scanFile(mPath, mMimeType);
} else {
mConnection.connect();
}
}
// start the scan when scanner is ready
public void onMediaScannerConnected() {
mConnection.scanFile(mPath, mMimeType);
}
public void onScanCompleted(String path, Uri uri) {
}
public void disConnected() {
if (mConnection.isConnected())
mConnection.disconnect();
}
}
我也是使用MediaScannerConnection,在4.4上親測可以實現更新媒體庫。構造裡的mime可以傳null