安卓简单联网程序之异步线程获得图片(通过URL)

通过输入URL来获得图片的简单联网安卓程序

Java文件 :(MainActivity.java)

package com.example.wxb201651538;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.widget.*;
import org.apache.http.conn.ConnectTimeoutException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
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 static androidx.lifecycle.ViewTreeLifecycleOwner.get;



public class MainActivity extends Activity {
private ImageView iv;
private EditText et_path;
private Bitmap bitmap;
private TextView tx;
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case 0x11:
iv.setImageBitmap((Bitmap) msg.obj);
break;
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.tx);
textView.setTextIsSelectable(true);
et_path = (EditText) findViewById(R.id.et_path);
iv = (ImageView) findViewById(R.id.iv);
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/**
* 新启动一个线程,封装一个获取图片url资源的方法,然后将接收的
* 图片放入Message发送到Handler中,最后在主线程中显示该图片。
*/
String path = et_path.getText().toString();
new Thread() {
@Override
public void run() {
try {
Bitmap result = get();
//定义一个Message
Message msg=Message.obtain();
msg.what=0x11;
msg.obj=result;
handler.sendMessage(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
});
}

private Bitmap get() throws Exception {
et_path = (EditText) findViewById(R.id.et_path);
String path = et_path.getText().toString();//定义一个url输入流,将图片地址放入
URL url = new URL(path);
//定义一个 InputStream 获取url的输入流(或者直接将url.openStream放入decodeStream解析成bitmap)
InputStream is=url.openStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
return bitmap;
}
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">


<EditText
android:id="@+id/et_path"
android:layout_width="fill_parent"
android:layout_height="75dp"
android:background="#00000000"
android:hint="请输入图片路径,然后点击图片" />

<ImageView
android:id="@+id/iv"
android:layout_width="fill_parent"
android:layout_height="423dp"
android:scaleType="centerInside"
android:src="@drawable/mxbc" />

<TextView
android:id="@+id/tx"
android:layout_width="match_parent"
android:layout_height="215dp"
android:text="网址实例:(复制)\n
http://101.42.149.4/yuguotianqingzuimeili/tupian/baitu.png\n
http://101.42.149.4/yuguotianqingzuimeili/tupian/heitu.png\n
http://101.42.149.4/yuguotianqingzuimeili/tupian/weixiao.png"
android:textSize="20dp" />

</LinearLayout>

效果演示:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值