java.lang.SecurityException:权限拒绝:不允许仅在 KitKat 上发送广播 android.intent.action.MEDIA_MOUNTED

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24072489/
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 07:45:36  来源:igfitidea点击:

java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED on KitKat only

androidandroid-intentbroadcastreceiverandroid-broadcast

提问by The Nomad

I am using the DownloadManagerto download images off our server and I am placing the files in the externalFilesDir.

我正在使用DownloadManager从我们的服务器下载图像,并将文件放在externalFilesDir.

I am send out a broadcast intent because I don't want these downloaded images to appear in the gallery.

我发送了一个广播意图,因为我不希望这些下载的图像出现在图库中。

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + context.getExternalFilesDir(null))));

I only tested this on my Galaxy S3 Jelly Bean 4.3 prior and it was working, but when I tested it on KitKat 4.4it crashes the app.

我之前只在我的 Galaxy S3 Jelly Bean 4.3 上测试过它并且它可以工作,但是当我测试它时KitKat 4.4应用程序崩溃了。

Is there a better way to not have the files downloaded from the DownloadManager not appear in the phone gallery?

有没有更好的方法让从 DownloadManager 下载的文件不出现在手机库中?

Stack Trace

堆栈跟踪

06-05 17:34:41.940: E/AndroidRuntime(15410): FATAL EXCEPTION: main
06-05 17:34:41.940: E/AndroidRuntime(15410): Process: com.walintukai.lfdate, PID: 15410
06-05 17:34:41.940: E/AndroidRuntime(15410): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.DOWNLOAD_COMPLETE flg=0x10 pkg=com.walintukai.lfdate (has extras) } in com.walintukai.lfdate.SocketIOService@42359f40
06-05 17:34:41.940: E/AndroidRuntime(15410):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:778)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at android.os.Handler.handleCallback(Handler.java:733)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at android.os.Handler.dispatchMessage(Handler.java:95)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at android.os.Looper.loop(Looper.java:136)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at android.app.ActivityThread.main(ActivityThread.java:5057)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at java.lang.reflect.Method.invokeNative(Native Method)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at java.lang.reflect.Method.invoke(Method.java:515)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at dalvik.system.NativeStart.main(Native Method)
06-05 17:34:41.940: E/AndroidRuntime(15410): Caused by: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=15410, uid=10135
06-05 17:34:41.940: E/AndroidRuntime(15410):    at android.os.Parcel.readException(Parcel.java:1465)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at android.os.Parcel.readException(Parcel.java:1419)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:2390)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at android.app.ContextImpl.sendBroadcast(ContextImpl.java:1127)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:365)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at com.walintukai.lfdate.SocketIOService.onReceive(SocketIOService.java:111)
06-05 17:34:41.940: E/AndroidRuntime(15410):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:768)
06-05 17:34:41.940: E/AndroidRuntime(15410):    ... 9 more

回答by Lazy Ninja

It seems that google is trying to prevent this from KITKAT.
Looking at core/rest/AndroidManifest.xmlyou will notice that broadcast android.intent.action.MEDIA_MOUNTEDis protected now. Which means it is a broadcast that only the system can send.

似乎谷歌正试图阻止 KITKAT 发生这种情况。
查看core/rest/AndroidManifest.xml您会注意到广播android.intent.action.MEDIA_MOUNTED现在受到保护。这意味着它是一个只有系统可以发送的广播。

<protected-broadcast android:name="android.intent.action.MEDIA_MOUNTED" />

The following should work for all versions:

以下应该适用于所有版本:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    final Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    final Uri contentUri = Uri.fromFile(outputFile); 
    scanIntent.setData(contentUri);
    sendBroadcast(scanIntent);
} else {
    final Intent intent = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()));
    sendBroadcast(intent);
}


If the above is not working try the following:

如果上述方法不起作用,请尝试以下操作:

According to this postyou need another way to fix it.
Like using MediaScannerConnectionor ACTION_MEDIA_SCANNER_SCAN_FILE.

根据这篇文章,您需要另一种方法来修复它。
喜欢使用MediaScannerConnectionACTION_MEDIA_SCANNER_SCAN_FILE

MediaScannerConnection.scanFile(this, new String[] {

file.getAbsolutePath()},

null, new MediaScannerConnection.OnScanCompletedListener() {

public void onScanCompleted(String path, Uri uri)

{


}

});

回答by WonderSoftwares

I know it's late but try this, it works in every version:

我知道已经晚了,但试试这个,它适用于每个版本:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    Uri contentUri = Uri.fromFile(out); // out is your output file
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
} else {
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
        Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}