解决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卡的广播了,呵呵,写到这里就算是写完了,写的很乱,希望大家将就的看下,希望对你有帮助!
解决Android拍照保存在系统相册不显示的问题的更多相关文章
- Android拍照保存在系统相册不显示的问题
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri uri = Uri.fromFile(new File(& ...
- android将应用中图片保存到系统相册并显示
我应用到的场景是程序中在视频通讯时截图,将截图保存到本地相册中 /*** @param bmp 获取的bitmap数据 * @param picName 自定义的图片名*/ public static ...
- android 调用系统照相机拍照后保存到系统相册,在系统图库中能看到
需求: 调用系统照相机进行拍照,并且保存到系统相册,调用系统相册的时候能看到 系统相册的路径:String cameraPath= Environment.getExternalStorageD ...
- Android7.0调用系统相机拍照、读取系统相册照片+CropImageView剪裁照片
Android手机拍照.剪裁,并非那么简单 简书地址:[我的简书–T9的第三个三角] 前言 项目中,基本都有用户自定义头像或自定义背景的功能,实现方法一般都是调用系统相机–拍照,或者系统相册–选择照片 ...
- iOS Swift WisdomScanKit二维码扫码SDK,自定义全屏拍照SDK,系统相册图片浏览,编辑SDK
iOS Swift WisdomScanKit 是一款强大的集二维码扫码,自定义全屏拍照,系统相册图片编辑多选和系统相册图片浏览功能于一身的 Framework SDK [1]前言: 今天给大家 ...
- MTK Android Android数据保存到系统数据库
如果有留意Android中系统设置Settings里面的源码,你会发现代码中频繁用到了Settings.System操作,该类通过键值对的形式,将一些特定的值以全局的模式保存到Setting的数据库中 ...
- 一站式解决,Android 拍照 图库的各种问题.
在android开发中, 在一些编辑个人信息的时候,经常会有头像这么一个东西,就两个方面,调用系统相机拍照,调用系统图库获取图片.但是往往会遇到各种问题: 1.oom 2.图片方向不对 3.activ ...
- 彻底解决android拍照后无法显示的问题
这是对上篇"android 图片拍照,相册选图,剪切并显示"的文章之后的 改进 上一篇文章虽然能解决图片的拍照剪切以及显示,但是发现他有一个缺点, 如果该程序单独运行,貌似没有任何 ...
- [Android] 拍照、截图、保存并显示在ImageView控件中
近期在做Android的项目,当中部分涉及到图像处理的内容.这里先讲述怎样调用Camera应用程序进行拍照,并截图和保存显示在ImageView控件中以及遇到的困难和解决方法. PS:作者购买 ...
随机推荐
- C++默认构造函数
原文链接:http://wenku.baidu.com/link?url=Qh59sZlrT7dAZwjkKqhUiUU2yq2GZams7wEQ9ULkYC7FgArX5adcp1EXVw_jqjf ...
- ipconfig命令
C:\Windows\System32>ipconfig -all Windows IP 配置 主机名 . . . . . . . . . . . . . : LuJunTao 主 DNS 后缀 ...
- WIP Job > APP-WIP-25191 or Pending Close
使用 Close Discrete Jobs (FORM) 关闭工单,有一工单的状态一直为PENDING CLOSE 检查 PENDING MATERIAL TRANSACTION ,PENDING ...
- bzoj1497
这道题让我涨姿势了 对于这类问题,我们称作最大权闭合图问题 就是每个点都有一个点权,要求选择一个点集,其中每个点的指向的点也在点集中,使这样一个点权和最大 对于这种问题,我们添加源点s,汇点t 对于点 ...
- bzoj1670
第一道凸包 采用Andrew算法,不论实现还是理解都非常简单 ..] of longint; i,j,k,m,n:longint; ans:double; procedure swap ...
- net 内存泄露和内存溢出
一直以来都对内存泄露和内存溢出理解的不是很深刻.在网上看到了几篇文章,于是整理了一下自己对内存泄露和内存溢出的理解. 一.概念 内存溢出:指程序在运行的过程中,程序对内存的需求超过了超过了计算机分配给 ...
- bzoj 1226 [SDOI2009]学校食堂Dining(状压DP)
Description 小F 的学校在城市的一个偏僻角落,所有学生都只好在学校吃饭.学校有一个食堂,虽然简陋,但食堂大厨总能做出让同学们满意的菜肴.当然,不同的人口味也不一定相同,但每个人的口味都可以 ...
- codeforces Round #347 (Div. 2) C - International Olympiad
思路:从后往前一位一位的模拟,每次判断一下当前枚举的数是否之间枚举过了.或者当前枚举数过小,小于1989. #include<cstdio> #include<cstring> ...
- 如何将SQL Server运行到Windows Azure上
从2012年6月6日开始,Windows Azure上一些强大的新功能现在可用于预览,包括新的Windows Azure虚拟机(VM).其中有关Windows Azure虚拟机最强大的一件事是他们利用 ...
- JavaScript性能优化:度量、监控与可视化1
HTTP事务所需要的步骤: 接下来,浏览器与远程Web服务器通过TCP三次握手协商来建立一个TCP/IP连接,类似对讲机的Over(完毕) Roger(明白) TCP/IP模型 TCP即传输控制协议( ...