主动通知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();


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


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

King·Forward

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值