Android--消息机制--handler--getImageFromUrl--网络上获取图片


package com.jiangge.imageviewer;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
	ImageView imageView;
	EditText editPath;
	Handler hadHandler;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		imageView = (ImageView) findViewById(R.id.image_view);
		editPath = (EditText) findViewById(R.id.it_path);
		hadHandler = new Handler();
	}

	public void click(View view){
		final String path = editPath.getText().toString().trim();
		if (TextUtils.isEmpty(path)) {
			Toast.makeText(this,"路径不能为空", Toast.LENGTH_LONG).show();
			return;
		}
		new Thread(){
			public void run(){
				try {
					URL url = new URL(path);
					HttpURLConnection conn = (HttpURLConnection) url.openConnection();
					conn.setRequestMethod("GET");
					conn.setConnectTimeout(5000);
//					conn.setReadTimeout();
					conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
					int code = conn.getResponseCode();
					System.out.println("===>>>" + code);
					if (code == 200) {
						InputStream is = conn.getInputStream();
						final Bitmap bitmap = BitmapFactory.decodeStream(is);
						hadHandler.post(new Runnable() {
							
							@Override
							public void run() {
								imageView.setImageBitmap(bitmap);
							}
						});
						
					}else {
						Toast.makeText(MainActivity.this,"获取图片失败", Toast.LENGTH_LONG).show();
					}
				} catch (Exception e) {
					e.printStackTrace();
					Toast.makeText(MainActivity.this,"获取图片失败", Toast.LENGTH_LONG).show();
				}
			}
		}.start();
		
	}
}


布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/image_view"
        android:layout_weight="1000"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <EditText
        android:id="@+id/it_path"
        android:singleLine="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="http://g.hiphotos.baidu.com/image/w%3D2048/sign=fa4ce6add31b0ef46ce89f5ee9fc50da/f636afc379310a55ed977077b64543a983261085.jpg"
        android:hint="图片路径" >
    </EditText>

    <Button
        android:id="@+id/btn"
        android:onClick="click"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发送请求" >
    </Button>

</LinearLayout>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值