当前位置:网站首页>ImageView shows network pictures
ImageView shows network pictures
2022-06-25 01:05:00 【I am at odds with C】
<?xml version="1.0" encoding="utf-8" ?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/getmypic" android:layout_width="match_parent" android:layout_height="100dp" android:text=" A little bit " android:onClick="getmypic" /> <ImageView android:id="@+id/myimagetive" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
java
package com.commerce.myapplication; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.ImageView; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.ProtocolException; import java.net.URL; public class MainActivity extends AppCompatActivity { MyHandler myHandler; protected static final int CHANGE_UI = 1; ImageView imageView; class MyHandler extends Handler { @Override public void handleMessage(@NonNull Message msg) { super.handleMessage(msg); if (msg.what == CHANGE_UI) { Bitmap bitmap = (Bitmap) msg.obj; imageView.setImageBitmap(bitmap); } } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = findViewById(R.id.myimagetive); myHandler = new MyHandler(); } public void getmypic(View view) { imageView.setBackground(getResources().getDrawable(R.drawable.ic_launcher_background)); new Thread() { @Override public void run() { super.run(); try { URL url = new URL("https://p3.ssl.qhimgs1.com/t01d2446155618ebfc9.jpg"); HttpURLConnection httpURLConnection; httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setConnectTimeout(500); httpURLConnection.setRequestMethod("GET"); int code = httpURLConnection.getResponseCode(); if (code == 200) { InputStream inputStream = httpURLConnection.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); Message message = new Message(); message.what = 1; message.obj = bitmap; myHandler.sendMessage(message); } } catch (IOException e) { e.printStackTrace(); } } }.start(); } }
Green android
<uses-permission android:name="android.permission.INTERNET" />
边栏推荐
- Go语言运算符(第8课下)
- I brush the question I - copy the linked list with random pointer
- EVM简略
- Mobile security tool jar
- Add information on the left and add parts on the right of the status bar
- 生成订单30分钟未支付,则自动取消,该怎么实现?
- The basic principle and application of iterator and enhanced for
- I 刷题 I — 复制带随机指针的链表
- JSON file of China's provincial boundaries
- 扎克伯格上手演示四款VR头显原型机,Meta透露元宇宙「家底」
猜你喜欢

ros(25):rqt_image_view报错Unable to load plugin for transport ‘compressed‘, error string

打卡smart精灵#1,品牌不缺吸引力,产品本身实力如何?

I brush the question I - copy the linked list with random pointer

Text editor for QT project practice - Episode 10

【Redis实现秒杀业务③】超卖问题之乐观锁具体实现

丹麦技术大学首创将量子计算应用于能源系统潮流建模

热力图展示相关矩阵

移动安全工具-jar

2022安全员-C证考试模拟100题及在线模拟考试

Cobalt Strike安装教程
随机推荐
[redis realizes seckill service ②] solution to oversold problem
Using tcp/udp tools to debug the yeelight ribbon
Practical operation notes - notebook plus memory and ash cleaning
Technologie des fenêtres coulissantes en octets dans la couche de transmission
Use of file class filenamefilter & filefilter in io
[redis realizes seckill service ④] one order for one person, and cannot be purchased repeatedly
Tiktok wallpaper applet, starlight wallpaper applet version 2.0, upgraded version
Mobile security tool apktool
Scala IO read by character
EVM简略
How to reduce the font size of custom controls (optimize the round dot progress bar)
ros(24):error: invalid initialization of reference of type ‘xx’ from expression of type ‘xx’
Text editor of QT project practice ---------- episode 8
Input series
【Redis实现秒杀业务④】一人一单,不可重复购买
Xcode预览(Preview)显示List视图内容的一个Bug及解决
adb shell sendevent
Qiniu cloud uploads video to get the first frame of video
[micro service sentinel] real time monitoring | RT | throughput | concurrency | QPS
108 pages (40000 words) proposal for future apartment intelligent design platform project (version 2022)