不允许发送广播android

不允许发送广播android

本文介绍了java.lang.SecurityException异常:权限被拒绝:不允许发送广播android.intent.action.MEDIA_MOUNTED上奇巧只的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的是下载管理器图像下载了我们的服务器,我把这些文件中的 externalFilesDir

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

  sendBroadcast(新意图(Intent.ACTION_MEDIA_MOUNTED,Uri.parse(文件://+ context.getExternalFilesDir(空))));
 

我只测试了这个在我之前的Galaxy S3果冻豆4.3,这是工作,但是当我测试了奇巧4.4 崩溃的应用程序。

有没有更好的办法没有从下载管理器下载的文件不会出现在手机图库?

堆栈跟踪

  06-05 17:34:41.940:E / AndroidRuntime(15410):致命异常:主要
06-05 17:34:41.940:E / AndroidRuntime(15410):工艺:com.walintukai.lfdate,PID:15410
06-05 17:34:41.940:E / AndroidRuntime(15410):java.lang.RuntimeException的错误:接收广播意向{行为= android.intent.action.DOWNLOAD_COMPLETE FLG = 0x10的PKG = com.walintukai.lfdate(有额外) }在com.walintukai.lfdate.SocketIOService$1@42359f40
06-05 17:34:41.940:E / AndroidRuntime(15410):在android.app.LoadedApk $ ReceiverDispatcher $ Args.run(LoadedApk.java:778)
06-05 17:34:41.940:E / AndroidRuntime(15410):在android.os.Handler.handleCallback(Handler.java:733)
06-05 17:34:41.940:E / AndroidRuntime(15410):在android.os.Handler.dispatchMessage(Handler.java:95)
06-05 17:34:41.940:E / AndroidRuntime(15410):在android.os.Looper.loop(Looper.java:136)
06-05 17:34:41.940:E / AndroidRuntime(15410):在android.app.ActivityThread.main(ActivityThread.java:5057)
06-05 17:34:41.940:E / AndroidRuntime(15410):在java.lang.reflect.Method.invokeNative(本机方法)
06-05 17:34:41.940:E / AndroidRuntime(15410):在java.lang.reflect.Method.invoke(Method.java:515)
06-05 17:34:41.940:E / AndroidRuntime(15410):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789)
06-05 17:34:41.940:E / AndroidRuntime(15410):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
06-05 17:34:41.940:E / AndroidRuntime(15410):在dalvik.system.NativeStart.main(本机方法)
06-05 17:34:41.940:E / AndroidRuntime(15410):由:java.lang.SecurityException异常:权限被拒绝:不允许从PID = 15410,UID发送广播android.intent.action.MEDIA_MOUNTED = 10135
06-05 17:34:41.940:E / AndroidRuntime(15410):在android.os.Parcel.readException(Parcel.java:1465)
06-05 17:34:41.940:E / AndroidRuntime(15410):在android.os.Parcel.readException(Parcel.java:1419)
06-05 17:34:41.940:E / AndroidRuntime(15410):在android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:2390)
06-05 17:34:41.940:E / AndroidRuntime(15410):在android.app.ContextImpl.sendBroadcast(ContextImpl.java:1127)
06-05 17:34:41.940:E / AndroidRuntime(15410):在android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:365)
06-05 17:34:41.940:E / AndroidRuntime(15410):在com.walintukai.lfdate.SocketIOService $ 1.onReceive(SocketIOService.java:111)
06-05 17:34:41.940:E / AndroidRuntime(15410):在android.app.LoadedApk $ ReceiverDispatcher $ Args.run(LoadedApk.java:768)
06-05 17:34:41.940:E / AndroidRuntime(15410):9 ...更多
 

解决方案

看来,谷歌正试图prevent在奇巧这一点。
看核心/ REST / AndroidManifest.xml中你会发现,广播 android.intent.action.MEDIA_MOUNTED 现在受到保护。这意味着它是只有系统能发送广播。

 <保护广播机器人:名称=android.intent.action.MEDIA_MOUNTED/>
 

据你需要另一种方式来解决它这个职位。
如使用 MediaScannerConnection ACTION_MEDIA_SCANNER_SCAN_FILE

  MediaScannerConnection.scanFile(这一点,新的String [] {

file.getAbsolutePath()},

空,新MediaScannerConnection.OnScanCompletedListener(){

公共无效onScanCompleted(字符串路径,开放的URI)

{


}

});
 

I am using the DownloadManager to download images off our server and I am placing the files in the 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.4 it crashes the app.

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

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$1@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$1.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

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

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

According to this post you need another way to fix it.
Like using MediaScannerConnection or ACTION_MEDIA_SCANNER_SCAN_FILE.

MediaScannerConnection.scanFile(this, new String[] {

file.getAbsolutePath()},

null, new MediaScannerConnection.OnScanCompletedListener() {

public void onScanCompleted(String path, Uri uri)

{


}

});

这篇关于java.lang.SecurityException异常:权限被拒绝:不允许发送广播android.intent.action.MEDIA_MOUNTED上奇巧只的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 01:39