ImageView展示网络图片

<?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" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值