当前位置: 首页 > news >正文

Android 将图片网址url转化为bitmap

1. 图片网址url转化为bitmap

1.1. 方法一 通过 HttpURLConnection 请求

  要使用一个线程去访问,因为是网络请求,这是一个一步请求,不能直接返回获取,要不然永远为null,在这里得到BitMap之后记得使用Hanlder或者EventBus传回主线程,不过现在加载图片都是用框架了,很少有转化为Bitmap的需求

    /*** 通过 网络图片 url 获取图片 Bitmap* @param photoUrl 网络图片 url*/private void requestWebPhotoBitmap(String photoUrl) {new Thread(() -> {HttpURLConnection connection = null;try {URL bitmapUrl = new URL(photoUrl);connection = (HttpURLConnection) bitmapUrl.openConnection();connection.setRequestMethod("GET");connection.setConnectTimeout(5000);connection.setReadTimeout(5000);// 判断是否请求成功if (connection.getResponseCode() == 200) {Message hintMessage = new Message();hintMessage.what = HANDLER_START_DOWNLOAD;hintHandler.sendMessage(hintMessage);InputStream inputStream = connection.getInputStream();imgBitmap = BitmapFactory.decodeStream(inputStream);Message message = showHandler.obtainMessage();showHandler.sendMessage(message);} else {Message hintMessage = new Message();hintMessage.what = HANDLER_NET_ERROR;hintHandler.sendMessage(hintMessage);}} catch (IOException e) {e.printStackTrace();} finally {if (connection != null) connection.disconnect();}}).start();}/*** 设置提示*/private final Handler hintHandler = new Handler(Looper.getMainLooper()){@Overridepublic void handleMessage(Message msg) {if(msg.what == HANDLER_START_DOWNLOAD)Toast.makeText(MainActivity.this, "获取图片中,请稍等", Toast.LENGTH_SHORT).show();else if(msg.what == HANDLER_NET_ERROR)Toast.makeText(MainActivity.this, "网络错误,请重试", Toast.LENGTH_SHORT).show();}};/*** 展示图片*/@SuppressLint("HandlerLeak")private final Handler showHandler = new Handler(Looper.getMainLooper()) {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);ivPhoto.setImageBitmap(imgBitmap); //填充控件}};

1.2. 方法二 通过 Glide

1.2.1. java

    /*** 获取 网络图片 Bitmap* @param imgUrl 网络图片url*/private void requestWebPhotoBitmap(String imgUrl) {Toast.makeText(MainActivity.this, "获取图片中,请稍等", Toast.LENGTH_SHORT).show();Glide.with(MainActivity.this).asBitmap().load(imgUrl).into(new CustomTarget<Bitmap>() {@SuppressLint("ClickableViewAccessibility")@Overridepublic void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {imgBitmap = resource;ivPhoto.setImageBitmap(imgBitmap)}@Overridepublic void onLoadCleared(@Nullable Drawable placeholder) {}});}

1.2.2. kotlin

   Glide.with(this).asBitmap().load(paramBean.userImg).into(object : CustomTarget<Bitmap?>() {override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap?>?) {val bitmap = resource}override fun onLoadCleared(placeholder: Drawable?) {}})

1.3. 调用

    private Bitmap imgBitmap = null;private ImageView ivPhoto;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ivPhoto = (ImageView) findViewById(R.id.photo);String imgUrl = "https://w.wallhaven.cc/full/l3/wallhaven-l3xk6q.jpg";requestWebPhotoBitmap(imgUrl);}
http://www.lryc.cn/news/309759.html

相关文章:

  • 鸿蒙Harmony应用开发—ArkTS声明式开发(通用属性:颜色渐变)
  • 腾讯云幻兽帕鲁游戏存档迁移教程,本地单人房迁移/四人世界怎么迁移存档?
  • C2_W2_Assignment_吴恩达_中英_Pytorch
  • C语言实现航班管理
  • 【Java面试题】SpringBoot与Spring的区别
  • 网络编程(IP、端口、协议、UDP、TCP)【详解】
  • Linux线程(二)----- 线程控制
  • Linux 内核irq_stack遍历
  • GIT问题记录
  • AzerothCore安装记录
  • Infineon_TC264智能车代码初探及C语言深度学习(一)
  • [Redis]——初识Redis
  • YTM32的同步串行通信外设SPI外设详解(Master Part)
  • 【C语言】三子棋
  • Web组态可视化编辑器 快速绘制组态
  • WebServer -- 注册登录
  • C3_W2_Collaborative_RecSys_Assignment_吴恩达_中英_Pytorch
  • Elasticsearch使用function_score查询酒店和排序
  • iOS消息发送流程
  • 【接口测试】常见HTTP面试题
  • 服务器硬件基础知识
  • matlab实现层次聚类与k-均值聚类算法
  • 【机器学习】包裹式特征选择之递归特征消除法
  • 【ArcGIS】重采样栅格像元匹配问题:不同空间分辨率栅格数据统一
  • Qt 简约又简单的加载动画 第七季 音量柱风格
  • 【JS】数值精度缺失问题解决方案
  • c++基础知识补充4
  • leetcode230. 二叉搜索树中第K小的元素
  • 医学大数据|文献阅读|有关“肠癌+机器学习”的研究记录
  • Linux信号【systemV】