Android将Bitmap保存至本地缓存,网络图片转Bitmap

使用Glide工具,在build.gradle添加Glide,大括号里面不用写,我这里写是因为和我的其它第三方有冲突
implementation("com.github.bumptech.glide:glide:4.11.0") {
    exclude group: "com.android.support"
}
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

获取Bitmap

Glide.with(getActivity())
        .asBitmap()
        .load("网络图片地址")
        .into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                uri = bitmap2uri(getActivity(), resource);
            }
        });

传入上下文参数和Bitmap,保存至本地缓存目录,返回Uri对象

private Uri bitmap2uri(Context c, Bitmap b) {//c.getCacheDir()
    //   /Android/data/你的报名/cache/1600739295328.jpg
    File path = new File(c.getExternalCacheDir() + File.separator + System.currentTimeMillis() + ".jpg");
    L.e("getAbsolutePath==="+path.getAbsolutePath()+"     ===getAbsolutePath==="+path.getParent());
    try {
        OutputStream os = new FileOutputStream(path);
        b.compress(Bitmap.CompressFormat.JPEG, 100, os);
        os.close();
        return Uri.fromFile(path);
    } catch (Exception ignored) {
    }
    return null;
}

Uri转Bitmap,并复制bitmap

Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
bmcopy = bitmap.copy(Bitmap.Config.ARGB_8888, true);
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值