不知道大家有在保存图片到图库时有这种经历:
图片存了两份一份压缩了一份没有
图库的Recent(最近)里找不到图片
图库时间不对
大家可能在用系统自带的Android插入图库方法:
MediaStore.Images.Media.insertImage in android.provider package of API 28
这个方法是系统提供给我们的插入图库的方法。先来看看它的源代码:
/**
* 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; /* 返回值 */
try {
url = cr.insert(EXTERNAL_CONTENT_URI, values);//将刚刚创建的ContentValues插入图库
if (source != null) {
OutputStream imageOut = cr.openOutputStream(url);
try