源码 [工程文件]:https://gitee.com/lwx001/ImageView

运行截图 :

 activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/ll_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="5dp"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/et_path"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginRight="3dp"
            android:layout_weight="1"
            android:background="#EBEBEB"
            android:hint="请输入图片路径"
            android:inputType="textUri"
            android:paddingLeft="3dp"
            android:textColor="#696969"
            android:textSize="20sp" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:background="#EBEBEB"
            android:onClick="click"
            android:text="浏览"
            android:textColor="#696969"
            android:textSize="20sp" />
    </LinearLayout>

    <ImageView
        android:id="@+id/iv_pic"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/ll_text"
        android:scaleType="centerCrop" />
</RelativeLayout>

MainActivity.java :

package cn.lwx.imageview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    protected static final int CHANGE_UI = 1;
    protected static final int ERROR = 2;
    private EditText et_path;
    private ImageView ivPic;
    // 主线程创建消息处理器
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            if (msg.what == CHANGE_UI) {
                Bitmap bitmap = (Bitmap) msg.obj;
                ivPic.setImageBitmap(bitmap);
            } else if (msg.what == ERROR) {
                Toast.makeText(MainActivity.this, "显示图片错误 !",
                        Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_path = (EditText) findViewById(R.id.et_path);
        ivPic = (ImageView) findViewById(R.id.iv_pic);
        //et_path.setText("http://www.photophoto.cn/m6/018/030/0180300388.jpg");
        //et_path.setText("图片地址!!!");
        //https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2834084951,2081126851&fm=11&gp=0.jpg

    }

    public void click(View view) {
        final String path = et_path.getText().toString().trim();
        if (TextUtils.isEmpty(path)) { //判断是否为空串
            Toast.makeText(this, "图片路径不能为空 !", Toast.LENGTH_SHORT).show();
        } else {
            //子线程请求网络,Android4.0以后访问网络不能放在主线程中
            new Thread() { //开启一个线程
                private HttpURLConnection conn;
                private Bitmap bitmap;

                public void run() { //联网操作
                    // 连接服务器 get 请求 获取图片
                    try {
                        // 创建URL对象
                        URL url = new URL(path);
                        // 根据url, 发送http的请求
                        conn = (HttpURLConnection) url.openConnection();
                        // 设置请求的方式
                        conn.setRequestMethod("GET");
                        // 设置超时时间
                        conn.setConnectTimeout(5000);
                        // 得到服务器返回的响应码
                        int code = conn.getResponseCode();
                        // 请求网络成功后, 返回码是200
                        if (code == 200) {
                            //获取输入流
                            InputStream is = conn.getInputStream();
                            //将流转换成Bitmap对象
                            bitmap = BitmapFactory.decodeStream(is);
                            //强制规定:必须在主线程中 更改图片
                            //将更改主界面的消息发送给主线程
                            Message msg = new Message();
                            msg.what = CHANGE_UI;//int型常量,区分不同的消息
                            msg.obj = bitmap;//通过消息携带对应的图片(object类型)
                            handler.sendMessage(msg);//发送消息
                        } else {
                            //返回码不等于200 请求服务器失败
                            Message msg = new Message();
                            msg.what = ERROR;
                            handler.sendMessage(msg);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Message msg = new Message();
                        msg.what = ERROR;
                        handler.sendMessage(msg);
                    }
                    //关闭连接
                    conn.disconnect();
                }
            }.start();
        }
    }
}

AndroidManifest.xml :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.lwx.imageview">
    <uses-permission android:name="android.permission.INTERNET"/>

    <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>

</manifest>

为善者,福虽未至,祸已远离~   嘻嘻(●'◡'●)

原文链接:https://blog.csdn.net/weixin_44949135/article/details/105025226

最后修改日期:2020年6月1日