http地址访问网络图片

MainActivity2.java

package com.example.myapplication;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity2 extends AppCompatActivity {
   // private int SHOWIMAGE = 5;
    private ImageView ivImage;
    private EditText  etImageUrl;
    private  Button btnView;


    private Handler handler=new Handler(){
        @SuppressLint("HandlerLeak")
        public void handleMessage(Message msg){
            switch (msg.what) {
                case 1:
                    Bitmap bitmap=(Bitmap) msg.obj;
                    ivImage.setImageBitmap(bitmap);
                    break;

                default:
                    break;
            }
        };
    };

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initViews();

    }
    private void initViews() {
        btnView=(Button) findViewById(R.id.btnView);
        etImageUrl=(EditText) findViewById(R.id.etImageUrl);
        ivImage= (ImageView) findViewById(R.id.ivImage);

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       // getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void showImage(View view){
        final String path=etImageUrl.getText().toString();
        if(TextUtils.isEmpty(path)){
            Toast.makeText(this, "图片路径不为空", Toast.LENGTH_LONG).show();
        }else{
            new Thread(){
                public void run(){
                    try {
                        URL url=new URL(path);
                        HttpURLConnection connection=(HttpURLConnection) url.openConnection();
                        connection.setRequestMethod("GET");
                        connection.setConnectTimeout(5000);
                        int responseCode=connection.getResponseCode();
                        if(responseCode==200){
                            InputStream inputStream=connection.getInputStream();
                            Bitmap bitmap= BitmapFactory.decodeStream(inputStream);
//ivImage.setImageBitmap(bitmap);
                            Message message=new Message();
                            message.what=1;
                            message.obj=bitmap;
                            handler.sendMessage(message);

                        }
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                };
            }.start();
        }
    }

}

layout_activity2

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/ivImage"
        android:layout_width="288dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="0.82" />

    <EditText
        android:id="@+id/etImageUrl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:text="https://pic1.zhimg.com/v2-e9cf6affcd58aa1480518556373e0d7c_r.jpg"
        android:hint="请输入图片的地址" />

    <Button
        android:id="@+id/btnView"
        android:layout_width="100dp"
        android:layout_height="48dp"
        android:layout_gravity="center"
        android:onClick="showImage"
        android:text="浏览"
        tools:ignore="OnClick" />

</LinearLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

信工院李平

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值