当前位置: 移动技术网 > IT编程>开发语言>其他编程 > Android将Bitmap保存至本地缓存,网络图片转Bitmap

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

2020年09月22日  | 移动技术网IT编程  | 我要评论
使用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'获取BitmapGlide.with(get.
使用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);

本文地址:https://blog.csdn.net/qq734048504/article/details/108726470

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网