Android4.4 refused to send intent.action_media_mounted scan SD card broadcast

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.