<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:text="加载" /> <ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" /> </LinearLayout>

网络类

package com.example.viewwebimagedemo.utils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL; public class HttpUtils { private final static String URL_PATH = "http://images.cnblogs.com/cnblogs_com/liuning8023/588559/o_Android.jpg"; public HttpUtils() {
// TODO Auto-generated constructor stub
} public static InputStream getImageViewInputStream() throws IOException {
InputStream inputStream = null; URL url = new URL(URL_PATH);
if (url != null) {
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
httpURLConnection.setConnectTimeout(3000);
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setDoInput(true);
int response_code = httpURLConnection.getResponseCode();
if (response_code == 200) {
inputStream = httpURLConnection.getInputStream();
}
}
return inputStream;
}
}
package com.example.viewwebimagedemo;

import java.io.InputStream;

import com.example.viewwebimagedemo.R;
import com.example.viewwebimagedemo.utils.HttpUtils; import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast; public class MainActivity extends Activity {
private Button button;
private ImageView imageView; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); button = (Button) this.findViewById(R.id.btn);
imageView = (ImageView) this.findViewById(R.id.imageView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(access).start();
}
});
}
// 另一个线程从网络获得图片
private Runnable access = new Runnable() {
@Override
public void run() {
getImg();
}
}; // 获得图片
public void getImg() {
try {
InputStream inputStream = HttpUtils.getImageViewInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Looper.prepare();// 必须调用此方法,要不然会报错
Message msg = new Message();
msg.what = 0;
msg.obj = bitmap;
handler.sendMessage(msg);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "获取图片错误", 1).show();
}
} // 更新UI信息
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0) {
if (msg.obj == null) {
Toast.makeText(getApplicationContext(), "图片不存在", 1).show();
} else {
Toast.makeText(getApplicationContext(), "加载图片...", 1)
.show();
setImg((Bitmap) msg.obj);
}
}
}
};
// 加载图片
private void setImg(Bitmap bm) {
imageView.setImageBitmap(bm);
}
}

效果:

记得加入权限:  <uses-permission android:name="android.permission.INTERNET" />

android 加载网络图片的更多相关文章

  1. Android加载网络图片报android.os.NetworkOnMainThreadException异常

    Android加载网络图片大致可以分为两种,低版本的和高版本的.低版本比如4.0一下或者更低版本的API直接利用Http就能实现了: 1.main.xml <?xml version=" ...

  2. Android加载网络图片学习过程

    好多应用,像我们公司的<乘友>还有其他的<飞鸽><陌陌><啪啪>这些,几乎每一款应用都需要加载网络图片,那ToYueXinShangWan,这是比须熟练 ...

  3. Android加载网络图片的工具类

    ImageView加载网络的图片 HttpUtil.java package com.eiice.httpuimagetils; import java.io.ByteArrayOutputStrea ...

  4. Android 加载网络图片设置到ImageView

    下载图片后显示在ImageView中 //1.定义全局变量 private Handler handler; private String image_url; private Bitmap bitm ...

  5. Android Volley入门到精通:使用Volley加载网络图片

    在上一篇文章中,我们了解了Volley到底是什么,以及它的基本用法.本篇文章中我们即将学习关于Volley更加高级的用法,如何你还没有看过我的上一篇文章的话,建议先去阅读Android Volley完 ...

  6. Android三种基本的加载网络图片方式(转)

    Android三种基本的加载网络图片方式,包括普通加载网络方式.用ImageLoader加载图片.用Volley加载图片. 1. [代码]普通加载网络方式 ? 1 2 3 4 5 6 7 8 9 10 ...

  7. android官方开源的高性能异步加载网络图片的Gridview例子

    这个是我在安卓安卓巴士上看到的资料,放到这儿共享下.这个例子android官方提供的,其中讲解了如何异步加载网络图片,以及在gridview中高效率的显示图片此代码很好的解决了加载大量图片时,报OOM ...

  8. Android中用双缓存技术,加载网络图片

    最近在学校参加一个比赛,写的一个Android应用,里面要加载大量的网络图片,可是用传统的方法图片一多就会造成程序出现内存溢出而崩溃.因为自己也在学习中,所以看了很多博客和视频,然后参照这些大神的写源 ...

  9. wemall app商城源码Android之ListView异步加载网络图片(优化缓存机制)

    wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享wemall app商城源码Android之L ...

随机推荐

  1. EntityFramework优缺点

    高层视图: 改变在现有系统使用EntityFramework的优势是什么? • All -in-1框架的类映射表,需要编写映射代码, 并且是很难维护的. • 可维护性,易于理解的代码,无需创造大的数据 ...

  2. MQTT——java简单测试(二)

    服务端代码: package bsit.mqtt.demo.one_way; import org.eclipse.paho.client.mqttv3.MqttClient; import org. ...

  3. ebs r12 -- adadmin: error while loading shared libraries: libclntsh.so.10.1

    安装EBS R12.2增加中文字符集时,运行$AU_TOP/bin/adadmin出错: $ adadmin adadmin: error while loading shared libraries ...

  4. TP框架常用配置

    <?php// +----------------------------------------------------------------------// | ThinkPHP [ WE ...

  5. Oracle EBS APIs

    参考链接: http://blog.csdn.net/pan_tian/article/details/7754598 http://blog.itpub.net/26687597/viewspace ...

  6. [Android] ListView关于adapter多种view设置

    使用的关键点是在adapter覆盖两个方法 public int getItemViewType(int position) public int getViewTypeCount() 其它的可另go ...

  7. Ubuntu14.04台式机r8169有线网卡驱动问题

    由于台式在安装了Ubuntu14.04后插入网线后灯不亮,不能使用有线网. 查找资料发现原来是网卡驱动问题,解决办法如下: 1.查看网卡驱动名称 lspci -v 找到以太网连接的kernel dri ...

  8. win下命令行替代品Cmder

    Cmder简单使用小结 Cmder是一款Windows环境下非常简洁美观易用的cmd替代者,它支持了大部分的Linux命令. 从官网下载下来一个zip安装包,解压之后运行根目录的Cmder.exe即可 ...

  9. timus 1175. Strange Sequence 解题报告

    1.题目描述: 1175. Strange Sequence Time limit: 1.0 secondMemory limit: 2 MB You have been asked to disco ...

  10. Lucene.net

    模糊查询-〉数据库全文检索-〉Lucene 一元分词(lucene内置) Analyzer analyzer = new CJKAnalyzer(); TokenStream tokenStream ...