.xml

<?xml version="1.0" encoding="utf-8"?>

        

,java
package com.example.abc.imageview;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
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 ActionBarActivity {
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(){
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);
}
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 {
new Thread() {
private HttpURLConnection conn;
private Bitmap bitmap;

            public void run() {                try {                    URL url = new URL(path);                    conn = (HttpURLConnection) url.openConnection();                    conn.setRequestMethod("GET");                    conn.setConnectTimeout(5000);                    int code = conn.getResponseCode();                    if (code == 200) {                        InputStream is = conn.getInputStream();                        bitmap = BitmapFactory.decodeStream(is);                        Message msg = new Message();                        msg.what = CHANGE_UI;                        msg.obj = bitmap;                        handler.sendMessage(msg);                    } else {                        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();    }}

}

更多相关文章

  1. Android的Textview里面夹杂图片
  2. Android(安卓)图片添加水印
  3. android 拍照和上传
  4. android 扫描sdcard下的图片
  5. Android(安卓)自定义Ratingbar
  6. android支付宝 KeyFactory PrivateKey
  7. EditView中插入图片
  8. HttpHuiApplication--下载图片url,HttpURLConnection,简单版
  9. clock时钟

随机推荐

  1. Android属性动画
  2. android开机动画
  3. android 开源项目汇总
  4. Android获取系统时间
  5. android 学习资料
  6. android播放音乐
  7. android实现xml数据的解析
  8. Android CalendarView 使用
  9. Android DOM解析XML
  10. Android自定义弹窗进度条