【问题标题】:java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED on KitKat onlyjava.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED on KitKat only
【发布时间】:2014-07-27 04:08:22
【问题描述】:

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

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

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

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

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

堆栈跟踪

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

【问题讨论】:

  • 您的问题不明确:您是要防止文件出现在图库中,还是要确保它们出现在图库中?要防止某个目录中的文件出现在 Gallery(或其他媒体库)中,请将名为 .nomedia 的文件放在同一目录中。要立即显示文件,请按照以下答案中的说明运行媒体扫描。

标签: android android-intent broadcastreceiver android-broadcast


【解决方案1】:

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

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

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

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

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

根据这个post,您需要另一种方法来修复它。
比如使用MediaScannerConnectionACTION_MEDIA_SCANNER_SCAN_FILE

MediaScannerConnection.scanFile(this, new String[] {

file.getAbsolutePath()},

null, new MediaScannerConnection.OnScanCompletedListener() {

public void onScanCompleted(String path, Uri uri)

{


}

});

【讨论】:

  • @SiddharthVyas 根据文档 MediaScannerConnection API 应该从 API 级别 1 工作,所以所有版本的 android 都应该工作。 developer.android.com/reference/android/media/…
  • 使用低于 4.4.4 的 android 版本无法正常工作,也无法刷新图库中的图像和文件夹
  • 第二种解决方案有效。感谢您的回复。
  • 如果我无权访问应用程序源代码怎么办?
【解决方案2】:

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

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

【讨论】:

  • 如何在受保护的 void 中获取文件 onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data);
  • 谢谢,这对我的问题有所帮助:stackoverflow.com/questions/37996241/…
【解决方案3】:

作为变体

public static void refreshDir(@NonNull Context context, @NonNull File dir) {

    notifyFileDeleted(context, dir);

    File[] files = dir.listFiles();

    if (files != null) {
        for (File file : files) {
            if (file.isDirectory())
                refreshDir(context, file);
            else
                notifyFileCreated(context, file);
        }
    }
}

public static void notifyFileCreated(@NonNull Context context, @NonNull File file) {
        //disable notification for non-existent files, folders and files from a location inaccessible via mtp
        if (file.exists() && file.isFile() && file.getAbsolutePath().indexOf("/data/data/") != 0) {
            MediaScannerConnection.scanFile(context, new String[]{file.getAbsolutePath()}, null, null);
            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
        }
    }

public static void notifyFileDeleted(@NonNull Context context, @NonNull File file) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            context.getApplicationContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.fromFile(file)));
        }
        else{
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri uri = Uri.fromFile(file);
            intent.setData(uri);
            context.sendBroadcast(intent);
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    相关资源
    最近更新 更多