权限拒绝:不允许在 android 中发送广播

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21469431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:37:23  来源:igfitidea点击:

Permission Denial: not allowed to send broadcast in android

androidandroid-camera

提问by TheDevMan

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.

我创建了一个简单的相机应用程序。它适用于除 Android 4.4 以外的所有 Android 版本。当我从我的相机应用程序拍照时出现以下错误。

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?

如何解决 KITKAT 的这个问题?

Let me know!

让我知道!

Thanks!

谢谢!

回答by CommonsWare

How to fix this issue for KITKAT?

如何解决 KITKAT 的这个问题?

That code has never been appropriate. Fortunately, Android is (finally) taking steps to prevent apps from spoofing more system broadcasts like this.

那个代码从来都不合适。幸运的是,Android(终于)采取措施防止应用程序像这样欺骗更多的系统广播。

If you want to tell Android to index a file you put on external storage, either use MediaScannerConnectionor ACTION_MEDIA_SCANNER_SCAN_FILE.

如果您想让 Android 为您放在外部存储设备上的文件建立索引,请使用MediaScannerConnectionACTION_MEDIA_SCANNER_SCAN_FILE.

回答by Carl

I resolved this problem, you can use this:

我解决了这个问题,你可以使用这个:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mediaMountUri));

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mediaMountUri));

hope this can help you !

希望这可以帮到你 !

回答by Yvan RAJAONARIVONY

Android prevents apps from sending broadcast like ACTION_MEDIA_SCANNER_SCAN_FILE or ACTION_MEDIA_MOUNTED. Use static method scanFile of MediaScannerConnection instead.

Android 会阻止应用发送像 ACTION_MEDIA_SCANNER_SCAN_FILE 或 ACTION_MEDIA_MOUNTED 这样的广播。请改用 MediaScannerConnection 的静态方法 scanFile。

MediaScannerConnection.scanFile(getApplicationContext(), new String[] { file.getAbsolutePath() }, null, new OnScanCompletedListener() {

                @Override
                public void onScanCompleted(String path, Uri uri) {
                    // TODO Auto-generated method stub

                }
            });

回答by Jon C. Hammer

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.

许多应用程序曾经使用这种意图在仅更改一个文件后重新扫描整个文件系统,这会大大消耗电池寿命。从 Android 4.4 开始,现在只有系统应用可以使用它。您可能必须找到不使用此意图的解决方法。