可以使用 Glide 来加载网络图片并获取 Bitmap,具体步骤如下:

  1. 导入 Glide 库

在 build.gradle 文件中添加以下依赖:

dependencies {
    implementation 'com.github.bumptech.glide:glide:4.11.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
}
  1. 加载网络图片并获取 Bitmap

使用 Glide 的 asBitmap() 方法加载图片并获取 Bitmap,代码示例如下:

Glide.with(this)
    .asBitmap()
    .load('https://www.example.com/image.jpg')
    .into(new SimpleTarget<Bitmap>() {
        @Override
        public void onResourceReady(@NonNull Bitmap bitmap, @Nullable Transition<? super Bitmap> transition) {
            // 获取到 Bitmap
        }
    });

这里的参数 this 是上下文对象,可以是 Activity 或 Fragment,也可以是 ApplicationContext。

load() 方法中传入图片的 url,SimpleTarget 的泛型指定返回的 Bitmap 类型,在 onResourceReady() 方法中获取到 Bitmap 对象。

Android Glide 加载网络图片并获取 Bitmap

原文地址: https://www.cveoy.top/t/topic/oR0E 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录