保存url到本地,刷新图库,解决保存图片不显示

//直接上代码
GlideUtils.loadImageAndSave(mContext, imgUrl, new GlideUtils.Listener() {
                                        @Override
                                        public void OnSuccess(String path) {
                                            // 通知相册有新图片,不通知保存后会出现相册不显示的情况,做重启之类操作会显示,或做其他刷新媒体操作
                                            File file = new File(path);
                                            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                                            Uri uri = Uri.fromFile(file);
                                            intent.setData(uri);
                                            ImagePagerActivity.this.sendBroadcast(intent);
                                        }

                                        @Override
                                        public void OnError() {

                                        }
                                    });
class GlideUtils{
/**
     * 加载并下载
     */
  public static void loadImageAndSave(Context context, String url, final Listener mListener) {
        RequestOptions options = new RequestOptions();
        options.placeholder(R.mipmap.head);
        options.diskCacheStrategy(DiskCacheStrategy.ALL);
        options.error(R.mipmap.head_defaul);
        try {
            Glide.with(context).load(url).apply(options).into(new SimpleTarget() {
                @Override
                public void onResourceReady(@NonNull Drawable resource, @Nullable Transition transition) {
                    try {
                        savaBitmap(System.currentTimeMillis() + ".jpg", Bitmap2Bytes(drawableToBitmap(resource)), mListener);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }catch (Exception e){}
    }

  public static byte[] Bitmap2Bytes(Bitmap bm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

  public interface Listener{
        void OnSuccess(String path);
        void OnError();
    }

// 保存图片到手机指定目录
    public static void savaBitmap(String imgName, byte[] bytes, Listener mListener) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            String filePath = null;
            FileOutputStream fos = null;
            try {
                filePath = Environment.getExternalStorageDirectory().getCanonicalPath() + "/NewImg";
                File imgDir = new File(filePath);
                if (!imgDir.exists()) {
                    imgDir.mkdirs();
                }
                imgName = filePath + "/" + imgName;
                fos = new FileOutputStream(imgName);
                fos.write(bytes);
                Toast.makeText(context, "图片已保存到" + filePath, Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fos != null) {
                        fos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            Log.i("-----", "savaBitmap: =============" + imgName);
            mListener.OnSuccess(imgName);
            return;
        } else {
            Toast.makeText(context, "请检查SD卡是否可用", Toast.LENGTH_SHORT).show();
        }
        mListener.OnError();
    }
}

你可能感兴趣的:(保存url到本地,刷新图库,解决保存图片不显示)