主界面布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="别点"
android:textSize="30dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/image"/>
</LinearLayout>
Java文件
package com.example.myapplication;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
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.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
Button button;//全局声明
ImageView imageView;
String URL= "https://img-ask.csdn.net/upload/201705/11/1494469948_803803.png?x-oss-process=image/resize,m_lfit,w_300/format,png";//图片地址设为字符串
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button);//绑定两个布局控件
imageView=(ImageView)findViewById(R.id.image);
button.setOnClickListener(new View.OnClickListener() {//对按键进行监听,当按下的时候启动另一个进程
@Override
public void onClick(View v) {
thread thread=new thread();
thread.start();
}
});
}
private class thread extends Thread{//不能在UI进程那里做这种操作,要另起一个进程,不然UI进程会卡住(创建类的时候要在最大括号里面创建)
public void run(){
super.run();
try {
URL url=new URL(URL);//创建URL通信方式对象url,而且利用URL地址链接建立URL实例(将字符串转为URL通信字符)
URLConnection urlConnection=url.openConnection();//对URL的实例使用openConnection方法(手动启动连接url),建立URLConnection
InputStream ins=urlConnection.getInputStream();//建立传输通道nputstream,并接收数据
bitmap= BitmapFactory.decodeStream(ins);//如果是图片,将收到的数据转换成Bitmap
Message message=new Message();//在需要的地方(一般在非UI进程发,在UI进程中收),创建Message对象,并设置其相关参数,再调用handler.sendMessage(message)发送消息给Handler处理,然后handle就会处理的了
message.what=123;
handler.sendMessage(message);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Handler handler=new Handler(){//处理者,即消息传递机制。通过Handler来接收运行线程所传递的信息并更新UI界面的Imageview
@Override
public void handleMessage(Message message)//处理消息的方法。该方法在创建Handler时重写。处理消息
{
super.handleMessage(message);
switch (message.what){
case 123:
imageView.setImageBitmap(bitmap);//如果从非UI进程中接收到的信息是123,便让图片显示
break;
}
}
};
}
增加权限
```cpp
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>