//网络请求要在子线程中请求
new Thread(){
@Override
public void run() {
try {
//网络请求
Request request = new Request.Builder().url(url).build();
//请求到资源转换为输入流
InputStream inputStream = okHttpClient.newCall(request).execute().body().byteStream();
//利用工厂类解析输入流的数据为BitMap
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
//发送消息给主线程
Message message = Message.obtain();
//暗号
message.what = 1;
//数据
message.obj = bitmap;
//发送
handler.sendMessage(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
利用Handler通知Ui线程更新数据
//创建Handler 来更新数据
Handler handler = new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(@NonNull Message msg) {
//拿到数据转化为BitMap
Bitmap obj = (Bitmap) msg.obj;
//给页面更新数据
imageView.setImageBitmap(obj);
}
};
标签:原生,obj,BitMap,Handler,new,message,解析,数据
From: https://www.cnblogs.com/loulan/p/16980605.html