以前可以通过
Intent.ACTION_MEDIA_MOUNTED发送广播。
4.4以后再这么用,就会报异常了:
SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from
.pid=xxx,uid=xxx
这是因为非系统应用不允许发送系统广播了(应该算是吧?)
方法一:
可以采用:
MediaScannerConnection.scanFile(this, new String[]{file.getPath()}, null, new MediaScannerConnection.OnScanCompletedListener() { @Override public void onScanCompleted(String path, Uri uri) { Log.d("TAG","加载完成"); } });
注意:第二个参数,不能用目录代替。
Environment.getExternalStorageDirectory().getPath()
这样就不行。
方法二:
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); File f = new File(photoFile.getAbsolutePath()); Uri contentUri = Uri.fromFile(f); mediaScanIntent.setData(contentUri);
参考链接:http://stackoverflow.com/questions/18624235/android-refreshing-the-gallery-after-saving-new-images