手动通知扫描SD卡主动生成缩略图
最近做项目遇到的难题,调用系统拍照获取不到缩略图,非得关机重启才会生成,所以我们要主动通知系统扫描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执行具体文件或文件夹进行扫描。
Intent mediaScanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
String path = "file://"
+ out.getPath();
Uri contentUri = Uri.parse(path); //out is your output file
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
MediaScannerReceiver 源码中的广播接收的部分代码:
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Uri uri = intent.getData();
String externalStoragePath = Environment.getExternalStorageDirectory().getPath(); if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
// scan internal storage
scan(context, MediaProvider.INTERNAL_VOLUME);
} else {
if (uri.getScheme().equals("file")) {
// handle intents related to external storage
String path = uri.getPath();
if (action.equals(Intent.ACTION_MEDIA_MOUNTED) &&
externalStoragePath.equals(path)) {
scan(context, MediaProvider.EXTERNAL_VOLUME);
} else if (action.equals(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE) &&
path != null && path.startsWith(externalStoragePath + "/")) {
scanFile(context, path);
}
}
}
}
所以最后的代码是:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Intent mediaScanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
String path = "file://"
+ out.getPath();
Uri contentUri = Uri.parse(path); //out is your output file
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent); } else {
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
}
手动通知扫描SD卡主动生成缩略图的更多相关文章
- Android扫描SD卡中的文件
当android的系统启动的时候,系统会自动扫描sdcard内的多媒体文件,并把获得的信息保存在一个系统数据库中,以后在其他程序中如果想要访问多媒体文件的信息,其实就是在这个数据库中进行的,而不是直接 ...
- Android4.4中不能发送SD卡就绪广播
当在Android上进行图片的扫描功能开发时一般会使用:sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(“file:// ...
- Android 检测SD卡应用
Android 检测SD卡应用 // Environment.MEDIA_MOUNTED // sd卡在手机上正常使用状态 // ...
- Android 常见SD卡操作
目录 Android 常见SD卡操作 Android 常见SD卡操作 参考 https://blog.csdn.net/mad1989/article/details/37568667. [0.] E ...
- Samsung_tiny4412(驱动笔记01)----linux 3.5,U-Boot,Busybox,SD卡启动环境搭建
/*********************************************************************************** * * linux 3.5,U ...
- Android 获取SD卡的图片资源
首先我先获得SD卡下的根目录路径: privateString isSdcard(){ File sdcardDir=null; boolean isSDExist=Environment.getEx ...
- 嵌入式linux 实现mdev SD卡和U盘自己主动挂载和卸载的方法 mdev.conf
首先先參考这些博客做一些了解:http://linux.chinaunix.net/techdoc/install/2009/11/18/1144936.shtml http://www.cnblog ...
- 第36章 SDIO—SD卡读写测试
第36章 SDIO—SD卡读写测试 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/f ...
- 第36章 SDIO—SD卡读写测试—零死角玩转STM32-F429系列
第36章 SDIO—SD卡读写测试 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/f ...
随机推荐
- 《Javascript高级程序设计》阅读记录(五):第六章 上
这个系列以往文字地址: <Javascript高级程序设计>阅读记录(一):第二.三章 <Javascript高级程序设计>阅读记录(二):第四章 <Javascript ...
- 【LeetCode】004. Median of Two Sorted Arrays
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- C++对C语言的拓展(1)—— 引用
1.变量名 变量名实质上是一段连续存储空间的别名,是一个标号(门牌号): 通过变量来申请并命名内存空间: 通过变量的名字可以使用内存空间. 2.引用的概念 变量名,本身是一段内存的引用,即别名(ali ...
- Azure Blob存储更改缓存时间
Azure的Blob存储可以作为Http的服务来使用.很多客户已经把Blob作为图片存储的服务,这样稍作代码的修改,解决了图片服务器带宽.性能等多种问题. 但同时问题也出现了:在传统Http的服务中很 ...
- SPI驱动框架-1(DM8127 Linux2.6.37为例)
一.驱动程序结构 1.platform_device 文件:/arch/arm/mach-omap2/device.c static struct omap2_mcspi_platform_confi ...
- Oracle之into
), NVL() INTO SALE_ID, STORE_ID FROM SALEFROMSTORE WHERE ORDERID = IN_ORDER_ID; 这里要注意,into的时候是一个sele ...
- 西安电子科技大学第16届程序设计竞赛 G-小国的复仇
西安电子科技大学第16届程序设计竞赛网络同步赛 G-小国的复仇 2 链接:https://www.nowcoder.com/acm/contest/107/G来源:牛客网 题目描述 众所周知,汀老师是 ...
- canvas渐变
代码: 1 /** 2 * Created by Administrator on 2016/1/29. 3 */ 4 function draw(id){ 5 var canvas = docume ...
- Errors occurred during the build. Errors occurred during the build. Errors running builder 'JavaScript Validator' on XXX
选择项目--右键Properties--Builders--如果有则取消第一项“JavaScript Validator”的勾. http://blog.csdn.net/error_case/art ...
- 用nfs挂载内核时出错 ERROR: Cannot umount的解决办法
SMDK2440 # nfs 30000000 192.168.1.106:/work/nfs_root/uImage ERROR: resetting ...