1.创建界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.bz0209.xzx.MainActivity">
<ImageButton
android:id="@+id/main_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/main_text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/main_text3"
android:layout_toEndOf="@+id/main_text1"
android:layout_toRightOf="@+id/main_text1"
android:hint="请输入图片路径" />
<Button
android:id="@+id/main_text3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:hint="浏览"
android:onClick="click" />
</LinearLayout>
2.编写package com.example.bz0209.xzx;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import Java.io.IOException;
import java.io.InputStream;
import java.NET.HttpURLConnection;
import java.Net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private static final int GETM=1;
private EditText main_text2;
private ImageButton main_text1;
private Button main_text3;
private int SHOW_IMAGE;
private Handler handler=new Handler(){
public void handleMessage(Message msg){
switch (msg.what){
case GETM:
Bitmap bitmap=(Bitmap) msg.obj;
main_text1.setImageBitmap(bitmap);
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main_text2=(EditText)findViewById(R.id.main_text2);
main_text1=(ImageButton)findViewById(R.id.main_text1);
}
public void click(View view) {
final String path = main_text2.getText().toString();
if (TextUtils.isEmpty(path)) {
Toast.makeText(this, "图片路径不能为空", Toast.LENGTH_SHORT).show();
} else {
new Thread() {
public void run() {
try {
URL ur1 = new URL(path);
HttpURLConnection httpURLConnection = (HttpURLConnection) ur1.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setConnectTimeout(5000);
int responsecode = httpURLConnection.getResponseCode();
if (responsecode == 200) {
InputStream is = httpURLConnection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
// main_text1.setImageBitmap(bitmap);
Message message = new Message();
message.what=GETM;
message.obj=bitmap;
handler.sendMessage(message);
} else {
Toast.makeText(MainActivity.this, "显示图片失败", Toast.LENGTH_SHORT).show();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
};
}.start();
}
}
}mainactivity