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