当前位置:网站首页>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" />
边栏推荐
- 移动安全工具-dex2jar
- 108 pages (40000 words) proposal for future apartment intelligent design platform project (version 2022)
- Examination questions and mock examination for safety management personnel of hazardous chemical business units in 2022
- 2022r1 quick opening pressure vessel operation test questions and answers
- JSON file of China's provincial boundaries
- Apk slimming compression experience
- 2021-04-18
- Sanic service startup failed
- [microservices sentinel] sentinel quick start | building an image | starting the console
- adb shell sendevent
猜你喜欢

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

ros(24):error: invalid initialization of reference of type ‘xx’ from expression of type ‘xx’

2021-09-12

Rich text tables, lists, pictures

2022r1 quick opening pressure vessel operation test questions and answers

Examination questions and mock examination for safety management personnel of hazardous chemical business units in 2022

Simulation questions and answers of the latest national fire facility operator (senior fire facility operator) in 2022

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

Decoupling pages and components using lifecycle

Mobile security tool jar
随机推荐
Solution to network access packet loss of Tencent cloud international ECS
Input series
placeholder
Previous basic review (link)
2022熔化焊接与热切割复训题库模拟考试平台操作
51 single chip microcomputer multi computer communication
热力图展示相关矩阵
Network request -volley
The basic principle and application of iterator and enhanced for
Apk decompiled method (not confused)
2022安全员-C证考试模拟100题及在线模拟考试
How to quickly open traffic master for wechat applet
If the order has not been paid for 30 minutes, it will be automatically cancelled. How can I achieve this?
adb shell sendevent
移动安全工具-jarsigner
Wallpaper applet wechat applet
EVM简略
QT (35) - operate excel qxlsx qaxobject
【Redis实现秒杀业务④】一人一单,不可重复购买
Text border format and text block of rich text