当前位置:网站首页>ImageView展示网络图片
ImageView展示网络图片
2022-06-24 20:17:00 【我与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="点一点" 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(); } }
绿色的android
<uses-permission android:name="android.permission.INTERNET" />
边栏推荐
- 【微服务|Sentinel】实时监控|RT|吞吐量|并发数|QPS
- Scala classes inherit multiple attributes
- Applet opening traffic master
- Xcode预览(Preview)显示List视图内容的一个Bug及解决
- 图书馆管理系统代码源码(php+css+js+mysql) 完整的代码源码
- 108 pages (40000 words) proposal for future apartment intelligent design platform project (version 2022)
- The problem of multiple callback of video ads stimulated by applets (offcolse problem)
- EVM简略
- ros(24):error: invalid initialization of reference of type ‘xx’ from expression of type ‘xx’
- Syntax highlighting of rich text
猜你喜欢

Xcode preview displays a bug in the content of the list view and its solution

108 pages (40000 words) proposal for future apartment intelligent design platform project (version 2022)
![[microservices sentinel] cluster link | microservices cluster environment construction](/img/75/3cc6abf1e72d651b082415db9db46c.png)
[microservices sentinel] cluster link | microservices cluster environment construction

Text editor of QT project practice ---------- episode 11

图书馆管理系统代码源码(php+css+js+mysql) 完整的代码源码

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

生成订单30分钟未支付,则自动取消,该怎么实现?
![[redis realizes seckill service ④] one order for one person, and cannot be purchased repeatedly](/img/75/bc588eb4e54899e93cc7df62e45820.png)
[redis realizes seckill service ④] one order for one person, and cannot be purchased repeatedly

Apk decompiled method (not confused)

VNC viewer remote connection raspberry pie without display
随机推荐
2022R1快开门式压力容器操作考题及答案
EVM简略
51 single chip microcomputer multi computer communication
Syntax highlighting of rich text
Helm chart warehouse operation
Scala sample class case calculate
【微服务|Sentinel】实时监控|RT|吞吐量|并发数|QPS
redis + lua实现分布式接口限流实现方案
Wallpaper applet wechat applet
Leetcode 1248. Statistics of "graceful subarray" (harm, suddenly found that it can only enumerate violently)
【Redis实现秒杀业务②】超卖问题的解决方案
[redis realizes seckill business ③] specific implementation of optimistic lock for oversold problem
Apk slimming compression experience
Custom control - round dot progress bar (imitating one key acceleration in security guard)
adb shell getevent
Mobile security tool jarsigner
Text editor for QT project practice - Episode 10
Scala sample class
Leetcode 1248. 统计「优美子数组」(害,突然发现只会暴力枚举了)
The problem of multiple callback of video ads stimulated by applets (offcolse problem)