When scanning for images on Android is developed, it is generally used: sendbroadcast (new Intent (intent.action_media_mounted, Uri.parse ("file://" + Environment.getexternalstoragedirectory () + Picpath));
However, in Android4.4 and above, the execution of the above code occurs with an exception:W/activitymanager (498): Permission Denial:not allowed to send broadcast Android.intent.action.MEDIA_MOUNTED from pid=2269, uid=20016
Because Android4.4 restricts the system application to use the broadcast notification system to scan the SD card, the problem is thrown.
There are two ways of resolving this:
The first type:
if (Build. VERSION. SDK_int >= Build. VERSION_codes. KITKAT) {//If yes4.4And above version Intent mediascanintent = new Intent (Intent. ACTION_media_scanner_scan_file);Uri Contenturi = URI. FromFile(Mphotofile);//out is your output fileMediascanintent. SetData(Contenturi);Cameraactivity. this. Sendbroadcast(mediascanintent);} else {sendbroadcast (new Intent (Intent. ACTION_media_mounted, Uri. Parse("file://"+ Environment. getExternalStorageDirectory())));}
The second way:
if (Build. VERSION. SDK_int >= Build. VERSION_codes. KITKAT) {//To determine if the SDK version is not4.4or higher than4.4string[] paths = new String[]{environment. getExternalStorageDirectory(). toString()};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()));} else {intent = new Intent (Intent. ACTION_media_scanner_scan_file);Intent. SetData(Uri. FromFile(New File (path)));} mcontext. Sendbroadcast(Intent);}
The above two ways of pro-test effective, plainly, in the higher version, using the Mediascannerconnection class or Action_media_scanner_scan_file broadcast notification system scanning folder, in the lower version of the use of Action_media_ Mounted broadcast notification system.
I suggest using the way above, the above way simple and clear!!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android4.4 refused to send intent.action_media_mounted scan SD card broadcast