Android输入网址查看图片

public class MainActivity extends Activity {
	protected static final int show_image = 0;
	EditText edPath;
    ImageView ivName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edPath=(EditText) findViewById(R.id.editText1);
        ivName=(ImageView) findViewById(R.id.imageView1);
    }
    
    Handler handler=new Handler(){
    	public void handleMessage(Message msg){
    		switch(msg.what){
    		case 1:
    			Bitmap bitmap=(Bitmap) msg.obj;
    			ivName.setImageBitmap(bitmap);
    			break;
    		default:
    			super.handleMessage(msg);
    			break;
    		}
    	
    	}
    };

    public void viewImage(View view){
    	final String path=edPath.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 httpURLConnection=(HttpURLConnection) url.openConnection();
						httpURLConnection.setRequestMethod("GET");
						httpURLConnection.setReadTimeout(5000);
						int responsecode=httpURLConnection.getResponseCode();
						if(responsecode==200){
							InputStream in=url.openStream();
							Bitmap bitmap=BitmapFactory.decodeStream(in);
							Message msg=new Message();
							msg.what=1;
							msg.obj=bitmap;
							handler.sendMessage(msg);
						
							in.close();
						}else{
							Toast.makeText(MainActivity.this, "获取失败",Toast.LENGTH_LONG).show();
				
						}
					} catch (MalformedURLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
    			}
    		}.start();
    	}
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值