欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-11-12 10:01:22
可能大家都知道我们保存相册到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卡的广播了,呵呵,写到这里就算是写完了,写的很乱,希望大家将就的看下,希望对你有帮助!