保存图片到系统相册【刷新媒体库,更新相册】

package com.cardiar.xin_an_doctor_app.utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * content 保存图片
 */

public class SaveImageUtil {
    private String[] imageTypes = new String[]{".jpg", ".png", ".jpeg", "webp"};

    //保存图片
    public void saveImage(final String imageUrl, final Context context) {
        Log.e("图片", imageUrl);
        Glide.with(context).load(imageUrl).asBitmap().into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                // 创建目录
                File appDir = new File(Environment.getExternalStorageDirectory(), "GadImage");
                if (!appDir.exists()) {
                    appDir.mkdirs();
                }
                String imageType = getImageType(imageUrl); //获取图片类型
                //System.currentTimeMillis()随机数
                String name = null;//截取图片名
                try {
                    name = imageUrl.substring(imageUrl.lastIndexOf("/") + 1, imageUrl.lastIndexOf("."));
                } catch (Exception e) {
                    name = imageUrl.substring(imageUrl.lastIndexOf("/") + 1, imageUrl.length());
                }
                String fileName = name + System.currentTimeMillis() + "." + imageType;
                File file = new File(appDir, fileName);
                if (!file.exists()) {
                    //保存图片
                    try {
                        FileOutputStream fos = new FileOutputStream(file);
                        if (TextUtils.equals(imageType, "jpg")) {
                            imageType = "jpeg";
                        }
                        imageType = imageType.toUpperCase();
                        resource.compress(Bitmap.CompressFormat.valueOf(imageType), 100, fos);
                        fos.flush();
                        fos.close();
                        ToastUtils.showToast(context, "图片已保存到GadImage");

                    } catch (FileNotFoundException e) {
                        if (e.toString().endsWith(("(Permission denied)"))) {
                            ToastUtils.showToast(context, "请前往应用权限管理打开储存权限");
                        }
                    } catch (IOException e) {
                        MyLog.e("dfdf", "onResourceReady: ");

                    }

                    // 其次把文件插入到系统图库
                    try {
                        MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);

                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    // 最后通知图库更新
                    context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.getPath())));
                } else {
                    ToastUtils.showToast(context, "图片已存在");
                }

            }
        });
    }

    //图片类型
    private String getImageType(String imageUrl) {
        String imageType;
        if (imageUrl.endsWith(imageTypes[0])) {
            imageType = "jpg";
        } else if (imageUrl.endsWith(imageTypes[1])) {
            imageType = "png";
        } else {
            imageType = "jpeg";
        }
        return imageType;
    }

}

注意! 

原有的更新图库的方法已经失效,应该采用最新的:

/**
  * 通知android媒体库更新文件夹
  *
  * @param filePath ilePath 文件绝对路径,、/sda/aaa/jjj.jpg
  */
 public void scanFile(Context context, String filePath) {
     try {
         MediaScannerConnection.scanFile(context, new String[]{filePath}, null,
                 new MediaScannerConnection.OnScanCompletedListener() {
                     public void onScanCompleted(String path, Uri uri) {
                         Log.i("*******", "Scanned " + path + ":");
                         Log.i("*******", "-> uri=" + uri);
                     }
                 });
     } catch (Exception e) {
         e.printStackTrace();
     }
 }
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值