Glide下载图片

前景提要:新版Glide与旧版Glide下载图片的调用方式稍有不同。

旧版Glide请参考:Android 使用Glide下载图片的几种方式

新版Glide下载图片:

Glide.with(context).downloadOnly().load("网络图片URL").into(new SimpleTarget<File>() {
            @Override
            public void onResourceReady(@NonNull File resource, @Nullable Transition<? super File> transition) {
                if (resource != null) {
                    try {
                        //获取到下载得到的图片,进行本地保存
                        String folder = StorageUtils.getCacheDirectory(context).getAbsolutePath() + File.separator + "download";
                        File appDir = new File(folder, "pictures");
                        if (!appDir.exists()) {
                            appDir.mkdirs();
                        }
                        String fileName = System.currentTimeMillis() + ".png";
                        File destFile = new File(appDir, fileName);

                        boolean status = Utils.copy(resource, destFile);
                        if (status) {
                            Utils.addMediaStore(context, destFile, destFile.getAbsolutePath());
                            Toast.makeText(context, "下载成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(context, "下载失败", Toast.LENGTH_SHORT).show();
                        }
                    } catch (Exception e) {
                        LogUtils.e(e.toString());
                        Toast.makeText(context, "下载失败", Toast.LENGTH_SHORT).show();
                    }

                }
            }
        });

复制图片的工具方法如下:

/**
     * 复制文件
     *
     * @param source 输入文件
     * @param target 输出文件
     * @return 文件是否复制成功
     */
    public static boolean copy(File source, File target) {
        boolean status = true;
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(source);
            fileOutputStream = new FileOutputStream(target);
            byte[] buffer = new byte[1024];
            while (fileInputStream.read(buffer) > 0) {
                fileOutputStream.write(buffer);
            }
        } catch (Exception e) {
            e.printStackTrace();
            status = false;
        } finally {
            try {
                fileInputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return status;
    }

图片保存成功后,需要更新图册:

/**
     * 把文件插入到系统图库
     * @param context
     * @param targetFile 要保存的照片文件
     * @param path  要保存的照片的路径地址
     */
    public static void addMediaStore(Context context, File targetFile, String path) {

        ContentResolver resolver = context.getContentResolver();
        ContentValues newValues = new ContentValues(5);
        newValues.put(MediaStore.Images.Media.DISPLAY_NAME, targetFile.getName());
        newValues.put(MediaStore.Images.Media.DATA, targetFile.getPath());
        newValues.put(MediaStore.Images.Media.DATE_MODIFIED, System.currentTimeMillis() / 1000);
        newValues.put(MediaStore.Images.Media.SIZE, targetFile.length());
        newValues.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
        resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, newValues);
        MediaScannerConnection.scanFile(context, new String[]{path}, null, null);//刷新相册
    }

这个时候,就能实现下载网络图片且能够在系统图册中查看的功能了。

 

附注:以上内容为个人开发记要,如存在问题或其他更好的方法,欢迎随时留言~

 

 

 

 

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值