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

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

废话不多刷,看代码。

方式一,发送一个广播,

 

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();

 

来源:http://www.bozhiyue.com/anroid/boke/2016/0318/3524.html

posted @ 2014-10-30 10:34  V青山绿水  阅读(17376)  评论(0编辑  收藏  举报