Android 保存图片到图库

不知道大家有在保存图片到图库时有这种经历:

图片存了两份一份压缩了一份没有
图库的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
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值