Reputation: 5954
I have created a simple camera app. It works fine in all the versions of Android except Android 4.4. I get the following error when I take a picture from my camera App.
java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=26089, uid=10120
Error comes up:
In the following place:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
How to fix this issue for KITKAT?
Let me know!
Thanks!
Upvotes: 12
Views: 32716
Reputation: 571
Android prevents apps from sending broadcast like ACTION_MEDIA_SCANNER_SCAN_FILE or ACTION_MEDIA_MOUNTED. Use static method scanFile of MediaScannerConnection instead.
MediaScannerConnection.scanFile(getApplicationContext(), new String[] { file.getAbsolutePath() }, null, new OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
// TODO Auto-generated method stub
}
});
Upvotes: 5
Reputation: 250
I resolved this problem, you can use this:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mediaMountUri));
hope this can help you !
Upvotes: 10
Reputation: 1006614
How to fix this issue for KITKAT?
That code has never been appropriate. Fortunately, Android is (finally) taking steps to prevent apps from spoofing more system broadcasts like this.
If you want to tell Android to index a file you put on external storage, either use MediaScannerConnection
or ACTION_MEDIA_SCANNER_SCAN_FILE
.
Upvotes: 20
Reputation: 392
Short answer: You can't, at least not easily.
A lot of apps used to use this intent to rescan the entire filesystem after changing only one file, which drained battery life considerably. Starting from Android 4.4, only System apps can now use it. You'll likely have to find a workaround that doesn't use this intent.
Upvotes: 2