主动通知Android系统图库进行更新

来源:互联网 发布:淘宝投诉电话是多少 编辑:程序博客网 时间:2024/05/05 17:48

项目中遇到调用图库进行图片的选择,因为不能主动及时更新,遂实现代码调用实现主动及时更新。

废话不多刷,看代码。

方式一,发送一个广播,


sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.parse("file://"+fileSD_file)));



方式二,通过MediaScannerConnection 类


MediaScannerConnection.scanFile(context, new String[]{fileSD_file.toString()}, null, null);



方式三,也是通过MediaScannerConnection 类


MediaScannerConnection msc=new MediaScannerConnection(context,new MediaScannerConnectionClient(){
@Override
public void onMediaScannerConnected() {
// TODO Auto-generated method stub

}
@Override
public void onScanCompleted(String path, Uri uri) {
// TODO Auto-generated method stub

}
}); 
msc.connect();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
URL url = null;
try {
url = fileSD_file.toURL();
} catch (MalformedURLException e) {
e.printStackTrace();
}

MimeTypeMap mtm=MimeTypeMap.getSingleton();

msc.scanFile(fileSD_file.toString(), mtm.getMimeTypeFromExtension(mtm.getFileExtensionFromUrl(url.toString())));

//此句上面的一句可以,下面的一句也可以,都适合这种方法(已用颜色标示)。

// msc.scanFile(fileSD_file.getAbsolutePath(), null);

msc.disconnect();


尽情参考,写成blog以备自用,不喜勿喷。


原创粉丝点击