当前位置:网站首页>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" />
边栏推荐
猜你喜欢

Add information on the left and add parts on the right of the status bar

2022r1 quick opening pressure vessel operation test questions and answers

108页(4万字)未来公寓智能化设计平台项目方案建议书2022版

Decoupling pages and components using lifecycle

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

QT(35)-操作EXCEL-QXlsx-QAxObject
![[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

2022R1快开门式压力容器操作考题及答案

If the order has not been paid for 30 minutes, it will be automatically cancelled. How can I achieve this?

I 刷题 I — 复制带随机指针的链表
随机推荐
【微服务|Sentinel】Sentinel快速入门|构建镜像|启动控制台
Text editor of QT project practice ---------- episode 8
Xcode preview displays a bug in the content of the list view and its solution
Default methods for Scala sample classes
Sanic service startup failed
[micro service sentinel] real time monitoring | RT | throughput | concurrency | QPS
断言(assert)的用法
Previous basic review (link)
Apk decompiled method (not confused)
Sliding window technology based on byte in transport layer
Practical operation notes - notebook plus memory and ash cleaning
【微服务|Sentinel】实时监控|RT|吞吐量|并发数|QPS
Several ways for wechat applet to jump to the page are worth collecting
Scala sample class case calculate
Realization of MNIST handwritten numeral recognition
Simulation questions and answers of the latest national fire facility operator (senior fire facility operator) in 2022
ServerSocket and socket connection
Syntax highlighting of rich text
Previous basic review
2022 simulated 100 questions of safety officer-c certificate examination and online simulated examination