热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

android开发分享Android拍照保存在系统相册不显示的问题解决方法

可能大家都知道我们保存相册到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 null 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开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • 本文介绍了Swing组件的用法,重点讲解了图标接口的定义和创建方法。图标接口用来将图标与各种组件相关联,可以是简单的绘画或使用磁盘上的GIF格式图像。文章详细介绍了图标接口的属性和绘制方法,并给出了一个菱形图标的实现示例。该示例可以配置图标的尺寸、颜色和填充状态。 ... [详细]
  • Android系统源码分析Zygote和SystemServer启动过程详解
    本文详细解析了Android系统源码中Zygote和SystemServer的启动过程。首先介绍了系统framework层启动的内容,帮助理解四大组件的启动和管理过程。接着介绍了AMS、PMS等系统服务的作用和调用方式。然后详细分析了Zygote的启动过程,解释了Zygote在Android启动过程中的决定作用。最后通过时序图展示了整个过程。 ... [详细]
  • Android中高级面试必知必会,积累总结
    本文介绍了Android中高级面试的必知必会内容,并总结了相关经验。文章指出,如今的Android市场对开发人员的要求更高,需要更专业的人才。同时,文章还给出了针对Android岗位的职责和要求,并提供了简历突出的建议。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文介绍了为什么要使用多进程处理TCP服务端,多进程的好处包括可靠性高和处理大量数据时速度快。然而,多进程不能共享进程空间,因此有一些变量不能共享。文章还提供了使用多进程实现TCP服务端的代码,并对代码进行了详细注释。 ... [详细]
  • Python瓦片图下载、合并、绘图、标记的代码示例
    本文提供了Python瓦片图下载、合并、绘图、标记的代码示例,包括下载代码、多线程下载、图像处理等功能。通过参考geoserver,使用PIL、cv2、numpy、gdal、osr等库实现了瓦片图的下载、合并、绘图和标记功能。代码示例详细介绍了各个功能的实现方法,供读者参考使用。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • Html5-Canvas实现简易的抽奖转盘效果
    本文介绍了如何使用Html5和Canvas标签来实现简易的抽奖转盘效果,同时使用了jQueryRotate.js旋转插件。文章中给出了主要的html和css代码,并展示了实现的基本效果。 ... [详细]
  • 【shell】网络处理:判断IP是否在网段、两个ip是否同网段、IP地址范围、网段包含关系
    本文介绍了使用shell脚本判断IP是否在同一网段、判断IP地址是否在某个范围内、计算IP地址范围、判断网段之间的包含关系的方法和原理。通过对IP和掩码进行与计算,可以判断两个IP是否在同一网段。同时,还提供了一段用于验证IP地址的正则表达式和判断特殊IP地址的方法。 ... [详细]
  • MPLS VP恩 后门链路shamlink实验及配置步骤
    本文介绍了MPLS VP恩 后门链路shamlink的实验步骤及配置过程,包括拓扑、CE1、PE1、P1、P2、PE2和CE2的配置。详细讲解了shamlink实验的目的和操作步骤,帮助读者理解和实践该技术。 ... [详细]
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • 本文介绍了如何在Mac上使用Pillow库加载不同于默认字体和大小的字体,并提供了一个简单的示例代码。通过该示例,读者可以了解如何在Python中使用Pillow库来写入不同字体的文本。同时,本文也解决了在Mac上使用Pillow库加载字体时可能遇到的问题。读者可以根据本文提供的示例代码,轻松实现在Mac上使用Pillow库加载不同字体的功能。 ... [详细]
author-avatar
蓝色流星魂
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有