`
369540808
  • 浏览: 197763 次
文章分类
社区版块
存档分类
最新评论

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>


分享到:
评论

相关推荐

    玩转Android---组件篇---Handler的使用

    玩转Android---组件篇---Handler的使用玩转Android---组件篇---Handler的使用玩转Android---组件篇---Handler的使用

    Android-async-http-1.4.9.jar

    android-async-http-1.4.9.jar是一般使用Apache HTTP Client或者采用HttpURLConnect,但是直接使用这两个...所有的请求都是独立在UI主线程之外,通过回调方法处理请求结果,采用android Handler message 机制传递信息

    Android代码-android-weak-handler

    Android Weak Handler Memory safer implementation of android.os.Handler Problem Original implementation of Handler always keeps hard reference to handler in queue of execution. Any object in Message or...

    android-async-http-1.4.6

    android-async-http是专门针对Android在Apache的HttpClient基础上构建的异步http连接。所有的请求全在UI(主)线程之外执行,而callback使用了Android的Handler发送消息机制在创建它的线程中执行。

    Android 用handler机制联网获取图片.zip

    Android 用handler机制联网获取图片能在实体机和虚拟机上运行,无广告,多次点击会造成主线程阻塞,出现anr;未实现联网获取图片的功能。

    Android的消息处理机制--Looper,Handler

    详细描述了Android的消息处理机制中,Looper和handler类详解

    Android-Event-driver.zip_Looper_android_android Handler_event

    在上一篇文章《Android应用程序消息处理机制(Looper、Handler)分析》中,我们分析了Android应用程序的消息处理机制,本文将结合这种消息处理机制来详细分析Android应用程序是如何获得键盘按键消息的。

    android-async-http-1.4.9

    使用android-async-http这个库可以大大的简化操作,它是基于Apache’s HttpClient ,所有的请求都是独立在UI主线程之外,通过回调方法处理请求结果,采用android Handler message 机制传递信息

    android-async-http-1.4.4

    使用android-async-http这个库可以大大的简化操作,它是基于Apache’s HttpClient ,所有的请求都是独立在UI主线程之外,通过回调方法处理请求结果,采用android Handler message 机制传递信息

    android-async-http-1.4.3.jar 包

    使用android-async-http这个库可以大大的简化操作,它是基于Apache’s HttpClient ,所有的请求都是独立在UI主线程之外,通过回调方法处理请求结果,采用android Handler message 机制传递信息。

    Android-app.rar

    Android 用handler机制联网获取图片 Android 百宝箱 Android 简单的翻译软件 Android 精美仿开眼视频跨平台App Android 自定义数字密码输入键盘 Android 趣味答题应用 Android 音乐播放器 Android--AA记账助手 ...

    android-Handler的使用

    android Handler的使用,我也刚开始学习,从别处下载了给大家分享

    Android应用程序消息处理机制(Looper、Handler)分析

    Android应用程序消息处理机制(Looper、Handler)分析

    Android Handler 异步消息机制的例子

    Android Handler 异步消息机制的例子,详细讲解请看http://bbs.droidstouch.com/thread-70-1-1.html

    android计数器(Handler消息传递机制)

    程序功能说明:点击“开始”按钮后,文本框中的数字每隔1秒钟自动加1,当增加到100时就停止。 (Handler消息传递机制)

    模拟Android Handler机制Demo

    一个Android handler机制的Demo

    Android 异步消息处理机制 让你深入理解 Looper、Handler、Message三者关系 - Hongyang -

    摘要视图订阅标签: Android分类:目录(?Android 异步消息处理机制 让你深入理解 Looper、Handler、Message三者关系95220人

    Android-扩展BaseAdapter实现progressBar下载

    * 该Demo涉及了BaseAdapter,ProgressBar,Handler,Thread,线程池; * * Android-扩展BaseAdapter实现progressBar下载 * 1.创建listview的xml * 2.创建listview中填充的xml * 3.创建继承BaseAdapter的...

    android-framework-hal

    7 Android的Audio系统 7.1 Audio系统的综述 Audio系统架构和代码路径 7.2 meida库中Audio相关接口 Audio系统的本地核心接口,类的层次结构 7.3 Audio系统和上层接口 Audio系统的JAVA层次的接口 7.4 Audio...

    Android代码-Api-Error-Handler

    Introduction Declarative error handling.... @AutoHandler // let compiler know it should analyse your class public interface SimplerErrorListener { // your code has to implement this in

Global site tag (gtag.js) - Google Analytics