---
title: "Android4.4中拒绝发送Intent.ACTION_MEDIA_MOUNTED扫描SD卡的广播"
author: "scliu0718"
date: "2014-04-28T17:30:00Z"
tags: []
slug: "android4-4-intent-action-media-mounted-sd"
draft: false
lastmod: "2014-04-28T18:16:09Z"
id: 2056314
url: "https://scliu0718.iteye.com/blog/2056314"
---

# 通常在我们的项目中，可能会遇到写本地文件，最常用的就是图片文件，在这之后需要通知系统重新扫描SD卡， 在Android4.4之前也就是以发送一个Action为“Intent.ACTION\_MEDIA\_MOUNTED”的广播通知执行扫描。如下：

```
this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
```

但在Android4.4中，则会抛出以下异常：

# W/ActivityManager( 498): Permission Denial: not allowed to send broadcast android.intent.action.MEDIA\_MOUNTED from pid=2269, uid=20016 那是因为Android4.4中限制了系统应用才有权限使用广播通知系统扫描SD卡。 解决方式： 使用MediaScannerConnection执行具体文件或文件夹进行扫描。

```
MediaScannerConnection.scanFile(this, new String[]{Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/" + fileName}, null, null);
```