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

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

概述可能大家都知道我们保存相册到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,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);
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,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);
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拍照保存在系统相册不显示的问题解决方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1142548.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存