通过输入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>