Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED解决方法


最近项目中使用了github中开源的文件管理器,如下:

https://github.com/brucend/FileManager_Micode

在android4.4及以上版本的手机里运行,碰到如下异常:

<pre name="code" class="html">12-21 12:02:07.643: E/AndroidRuntime(28893): java.lang.RuntimeException: An error occured while executing doInBackground()
12-21 12:02:07.643: E/AndroidRuntime(28893): 	at android.os.AsyncTask$3.done(AsyncTask.java:300)
12-21 12:02:07.643: E/AndroidRuntime(28893): 	at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
12-21 12:02:07.643: E/AndroidRuntime(28893): 	at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
12-21 12:02:07.643: E/AndroidRuntime(28893): 	at java.util.concurrent.FutureTask.run(FutureTask.java:242)
12-21 12:02:07.643: E/AndroidRuntime(28893): 	at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-21 12:02:07.643: E/AndroidRuntime(28893): 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-21 12:02:07.643: E/AndroidRuntime(28893): 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-21 12:02:07.643: E/AndroidRuntime(28893): 	at java.lang.Thread.run(Thread.java:818)
12-21 12:02:07.643: E/AndroidRuntime(28893): <strong>Caused by: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=28893, uid=10249</strong>
12-21 12:02:07.643: E/AndroidRuntime(28893): 	at android.os.Parcel.readException(Parcel.java:1540)
12-21 12:02:07.643: E/AndroidRuntime(28893): 	at android.os.Parcel.readException(Parcel.java:1493)
12-21 12:02:07.643: E/AndroidRuntime(28893): 	at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:2795)
12-21 12:02:07.643: E/AndroidRuntime(28893): 	at android.app.ContextImpl.sendBroadcast(ContextImpl.java:1327)
12-21 12:02:07.643: E/AndroidRuntime(28893): 	at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:376)
12-21 12:02:07.643: E/AndroidRuntime(28893): 	at net.micode.fileexplorer.FileViewInteractionHub.notifyFileSystemChanged(FileViewInteractionHub.java:585)
12-21 12:02:07.643: E/AndroidRuntime(28893): 	at net.micode.fileexplorer.FileViewInteractionHub.onFileChanged(FileViewInteractionHub.java:1088)
12-21 12:02:07.643: E/AndroidRuntime(28893): 	at net.micode.fileexplorer.FileOperationHelper$4.run(FileOperationHelper.java:218)
12-21 12:02:07.643: E/AndroidRuntime(28893): 	at net.micode.fileexplorer.FileOperationHelper$3.doInBackground(FileOperationHelper.java:164)
12-21 12:02:07.643: E/AndroidRuntime(28893): 	at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-21 12:02:07.643: E/AndroidRuntime(28893): 	at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-21 12:02:07.643: E/AndroidRuntime(28893): 	... 4 more


 
 

问题所在:Android4.4及以上版本中限制了只有系统应用才有权限发送广播(android.intent.action.MEDIA_MOUNTED)通知系统扫描SD卡,非系统应用则无权限,一旦运行则出现上述异常。

解决方法:将net.micode.fileexplorer.FileViewInteractionHub.java文件中的notifyFileSystemChanged()修改如下:

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



你可能感兴趣的:(MEDIA_MOUNTED)