通知系统图库刷新

BUG:not allowed to send broadcast android.intent.action.MEDIA_MOUNTED
出现问题的原因:
Android4.4及以上版本中限制了只有系统应用才有权限发送广播(android.intent.action.MEDIA_MOUNTED)通知系统扫描SD卡,非系统应用则无权限,一旦运行则出现上述异常

有多种解决方案
一 更换action 亲测有效 百试不爽

 Intent intent;
        intent=new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));
        sendBroadcast(intent);

二判断系统版本(百度方案 可尝试 不保证有没有错)

private void notifyFileSystemChanged(String path) {  
    if (path == null)  
        return;  
    final File f = new File(path);  
    if (Build.VERSION.SDK_INT >= 19 /*Build.VERSION_CODES.KITKAT*/) { //添加此判断,判断SDK版本是不是4.4或者高于4.4  
        String[] paths = new String[]{Environment.getExternalStorageDirectory().toString()};  
        String[] paths = new String[]{path};  
        MediaScannerConnection.scanFile(mContext, paths, null, null);  
    } else {  
     final Intent intent;  
     if (f.isDirectory()) {  
         intent = new Intent(Intent.ACTION_MEDIA_MOUNTED);  
         intent.setClassName("com.android.providers.media", "com.android.providers.media.MediaScannerReceiver");  
         intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));  
         Log.v(LOG_TAG, "directory changed, send broadcast:" + intent.toString());  
     } else {  
         intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);  
         intent.setData(Uri.fromFile(new File(path)));  
         Log.v(LOG_TAG, "file changed, send broadcast:" + intent.toString());  
     }  
     mContext.sendBroadcast(intent);  
    }  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值