解决Android拍照保存在系统相册不显示的问题
可能大家都知道我们保存相册到Android手机的时候,然后去打开系统图库找不到我们想要的那张图片,那是因为我们插入的图片还没有更新的缘故,先讲解下插入系统图库的方法吧,很简单,一句代码就能实现
- MediaStore.Images.Media.insertImage(getContentResolver(), mBitmap, "", "");
通过上面的那句代码就能插入到系统图库,这时候有一个问题,就是我们不能指定插入照片的名字,而是系统给了我们一个当前时间的毫秒数为名字,有一个问题郁闷了很久,我还是先把insertImage的源码贴出来吧
- /**
- * Insert an image and create a thumbnail for it.
- *
- * @param cr The content resolver to use
- * @param source The stream to use for the image
- * @param title The name of the image
- * @param description The description of the image
- * @return The URL to the newly created image, or <code>null</code> if the image failed to be stored
- * for any reason.
- */
- public static final String insertImage(ContentResolver cr, Bitmap source,
- String title, String description) {
- ContentValues values = new ContentValues();
- values.put(Images.Media.TITLE, title);
- values.put(Images.Media.DESCRIPTION, description);
- values.put(Images.Media.MIME_TYPE, "image/jpeg");
- Uri url = null;
- String stringUrl = null; /* value to be returned */
- try {
- url = cr.insert(EXTERNAL_CONTENT_URI, values);
- if (source != null) {
- OutputStream imageOut = cr.openOutputStream(url);
- try {
- source.compress(Bitmap.CompressFormat.JPEG, 50, imageOut);
- } finally {
- imageOut.close();
- }
- long id = ContentUris.parseId(url);
- // Wait until MINI_KIND thumbnail is generated.
- Bitmap miniThumb = Images.Thumbnails.getThumbnail(cr, id,
- Images.Thumbnails.MINI_KIND, null);
- // This is for backward compatibility.
- Bitmap microThumb = StoreThumbnail(cr, miniThumb, id, 50F, 50F,
- Images.Thumbnails.MICRO_KIND);
- } else {
- Log.e(TAG, "Failed to create thumbnail, removing original");
- cr.delete(url, null, null);
- url = null;
- }
- } catch (Exception e) {
- Log.e(TAG, "Failed to insert image", e);
- if (url != null) {
- cr.delete(url, null, null);
- url = null;
- }
- }
- if (url != null) {
- stringUrl = url.toString();
- }
- return stringUrl;
- }
上面方法里面有一个title,我刚以为是可以设置图片的名字,设置一下,原来不是,郁闷,哪位高手知道title这个字段是干嘛的,告诉下小弟,不胜感激!
当然Android还提供了一个插入系统相册的方法,可以指定保存图片的名字,我把源码贴出来吧
- /**
- * Insert an image and create a thumbnail for it.
- *
- * @param cr The content resolver to use
- * @param imagePath The path to the image to insert
- * @param name The name of the image
- * @param description The description of the image
- * @return The URL to the newly created image
- * @throws FileNotFoundException
- */
- public static final String insertImage(ContentResolver cr, String imagePath,
- String name, String description) throws FileNotFoundException {
- // Check if file exists with a FileInputStream
- FileInputStream stream = new FileInputStream(imagePath);
- try {
- Bitmap bm = BitmapFactory.decodeFile(imagePath);
- String ret = insertImage(cr, bm, name, description);
- bm.recycle();
- return ret;
- } finally {
- try {
- stream.close();
- } catch (IOException e) {
- }
- }
- }
啊啊,贴完源码我才发现,这个方法调用了第一个方法,这个name就是上面方法的title,晕死,这下更加郁闷了,反正我设置title无效果,求高手为小弟解答,先不管了,我们继续往下说
上面那段代码插入到系统相册之后还需要发条广播
- sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
上面那条广播是扫描整个sd卡的广播,如果你sd卡里面东西很多会扫描很久,在扫描当中我们是不能访问sd卡,所以这样子用户体现很不好,用过微信的朋友都知道,微信保存图片到系统相册并没有扫描整个SD卡,所以我们用到下面的方法
- Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
- Uri uri = Uri.fromFile(new File("/sdcard/image.jpg"));
- intent.setData(uri);
- mContext.sendBroadcast(intent);
或者用MediaScannerConnection
- final MediaScannerConnection msc = new MediaScannerConnection(mContext, new MediaScannerConnectionClient() {
- public void onMediaScannerConnected() {
- msc.scanFile("/sdcard/image.jpg", "image/jpeg");
- }
- public void onScanCompleted(String path, Uri uri) {
- Log.v(TAG, "scan completed");
- msc.disconnect();
- }
- });
也行你会问我,怎么获取到我们刚刚插入的图片的路径?呵呵,这个自有方法获取,insertImage(ContentResolver cr, Bitmap source,String title, String description),这个方法给我们返回的就是插入图片的Uri,我们根据这个Uri就能获取到图片的绝对路径
- private String getFilePathByContentResolver(Context context, Uri uri) {
- if (null == uri) {
- return null;
- }
- Cursor c = context.getContentResolver().query(uri, null, null, null, null);
- String filePath = null;
- if (null == c) {
- throw new IllegalArgumentException(
- "Query on " + uri + " returns null result.");
- }
- try {
- if ((c.getCount() != 1) || !c.moveToFirst()) {
- } else {
- filePath = c.getString(
- c.getColumnIndexOrThrow(MediaColumns.DATA));
- }
- } finally {
- c.close();
- }
- return filePath;
- }
根据上面的那个方法获取到的就是图片的绝对路径,这样子我们就不用发送扫描整个SD卡的广播了,呵呵,写到这里就算是写完了,写的很乱,希望大家将就的看下,希望对你有帮助!
转载于:https://www.cnblogs.com/dongweiq/p/6215375.html
解决Android拍照保存在系统相册不显示的问题相关推荐
- android拍照保存到系统相册,调用系统相机拍照,并且保存到系统相册的一般套路...
最近遇到也一个需求:调用系统相机拍照,并且照片可以在系统相册显示. 关于照片显示在系统相册这个问题,因为安卓机型太多了,各大厂商都对原生系统进行了不同程度的定制,所以在一般情况下,有的手机会把图片直接 ...
- MUI 拍照和从系统相册选择图片上传
要完成用MUI 拍照和从系统相册选择图片上传的功能,可以理解成有三个功能 1 调用手机相机的功能(可以查看官方API http://www.html5plus.org/doc/zh_cn/camer ...
- (转)解决android开发人员,手机app图标显示不正确问题
(转)解决android开发人员,手机app图标显示不正确问题 参考文章: (1)(转)解决android开发人员,手机app图标显示不正确问题 (2)https://www.cnblogs.com/ ...
- ARFoundation系列讲解 - 56 录制屏幕并且保存到系统相册
一.介绍 录制视频我们使用的是 "NatCorder" 插件,"NatCorder" 是一个跨平台屏幕录制软件,可以指定录制视频的相机层级.在Android.i ...
- 小程序生成图片保存到系统相册
wxml: <view class="container"> <view class='page-section'> <view class=&quo ...
- Android之打开手机系统相册
1.需求 打开系统相册,获取图片进行扫描操作 2.代码实现 Intent pickIntent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Me ...
- android拍照保存照片方向,android 拍照的照片方向问题,读取图片EXIF信息
Exif是一种图象文件格式,它的数据存储与JPEG格式是完全相同的.实际上Exif格式就是在JPEG格式头部插入了数码照片的信息,包括拍摄时的光圈.快门.白平衡.ISO.焦距.日期时间等各种和拍摄条件 ...
- ARFoundation系列讲解 - 55 拍照并且保存到系统相册
一.Unity的截屏方式 1.使用 CaptureScreenshot Api ,这种方法只能截取全屏,一般很少使用这种方法. Application.CaptureScreenshot(" ...
- android 调用系统照相机拍照后保存到系统相册
http://blog.csdn.net/xiaanming/article/details/8990627 http://blog.csdn.net/jm_beizi/article/details ...
最新文章
- 预告|第四届工业大数据创新竞赛决赛答辩倒计时
- C语言 int 转单精度浮点,单精度浮点数与十六进制转换 C语言程序 单片机也可用...
- linux mkfs.ext3 参数,linux命令mkfs.ext3用法[阮胜昌]
- Hyperledger Fabric 核心模块(1)整体概述
- 前端学习(1937)vue之电商管理系统电商系统之渲染分配角色的对话框并请求数据
- codeup 1128: 出租车费 贪心|找规律
- (39)System Verilog程序Program设计实例
- 查询前10条_98条铁路!2021年底前计划开工建设铁路进度一览(10月8日更新)
- QT学习笔记之对话框
- 测试中常见bug总结
- python制作的游戏如何转化为swf_PYTHON实现swf提取
- vc 6.0添加c文件 fatal error C1010解决办法
- 《MYSQL必知必会》—2.MySQL简介
- 循环链表(约瑟夫环问题)
- 通过JS语句判断WEB网站的访问端是电脑还是手机
- 宝塔Linux控制面板专业版破解步骤【亲测可用】
- 自己用过最好用的pdf转word软件
- Prisma(一)——基础
- C语言统计元音字母数量
- java网上下载文件
热门文章
- python中的continue和break
- mysql 导出所有函数_mysql 导入导出 包括函数或者存储过程
- 使用Bert/ERNIE进行中文短文本分类(附数据集)
- android simple-xml,使用Maven构建Android项目-dexer在simple-xml依赖项上失败
- 如何开启outlook邮箱的pop3和smtp_怎么在电子邮件客户端上登录腾讯邮箱(QQ邮箱 )?
- Vue底层判断标签的性能优化方法
- Linux IO控制命令生成
- CentOS7下使用yum快速安装配置oracle数据库
- iptables第二部分
- 超越Hadoop的大数据分析之图形处理尺寸