android 加载网络图片
<?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 加载网络图片的更多相关文章
- Android加载网络图片报android.os.NetworkOnMainThreadException异常
Android加载网络图片大致可以分为两种,低版本的和高版本的.低版本比如4.0一下或者更低版本的API直接利用Http就能实现了: 1.main.xml <?xml version=" ...
- Android加载网络图片学习过程
好多应用,像我们公司的<乘友>还有其他的<飞鸽><陌陌><啪啪>这些,几乎每一款应用都需要加载网络图片,那ToYueXinShangWan,这是比须熟练 ...
- Android加载网络图片的工具类
ImageView加载网络的图片 HttpUtil.java package com.eiice.httpuimagetils; import java.io.ByteArrayOutputStrea ...
- Android 加载网络图片设置到ImageView
下载图片后显示在ImageView中 //1.定义全局变量 private Handler handler; private String image_url; private Bitmap bitm ...
- Android Volley入门到精通:使用Volley加载网络图片
在上一篇文章中,我们了解了Volley到底是什么,以及它的基本用法.本篇文章中我们即将学习关于Volley更加高级的用法,如何你还没有看过我的上一篇文章的话,建议先去阅读Android Volley完 ...
- Android三种基本的加载网络图片方式(转)
Android三种基本的加载网络图片方式,包括普通加载网络方式.用ImageLoader加载图片.用Volley加载图片. 1. [代码]普通加载网络方式 ? 1 2 3 4 5 6 7 8 9 10 ...
- android官方开源的高性能异步加载网络图片的Gridview例子
这个是我在安卓安卓巴士上看到的资料,放到这儿共享下.这个例子android官方提供的,其中讲解了如何异步加载网络图片,以及在gridview中高效率的显示图片此代码很好的解决了加载大量图片时,报OOM ...
- Android中用双缓存技术,加载网络图片
最近在学校参加一个比赛,写的一个Android应用,里面要加载大量的网络图片,可是用传统的方法图片一多就会造成程序出现内存溢出而崩溃.因为自己也在学习中,所以看了很多博客和视频,然后参照这些大神的写源 ...
- wemall app商城源码Android之ListView异步加载网络图片(优化缓存机制)
wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享wemall app商城源码Android之L ...
随机推荐
- [原创]Eclipse Mars 在Ubuntu升级后无法工作的解决方法
近日将自己的Ubuntu从14.04LTS升级到了16.04LTS,顿时发现Eclipse不能正常工作了,到Ubuntu的官网上转了一圈发现以下解决方案: 症状: [1]Eclipse启动很慢; [2 ...
- awk的数组使用经历
背景:之前是一个数学妞,所以操作系统类的就由windows系列霸占了,甚至“cmd"是什么东西,环境变量是什么概念......其实说那么多就是想表明一点:你现在很有可能比我知道得多得多呢! ...
- mui框架使用的过程中遇到的几个问题
1.zepto.js和mui一起使用的时候,tap事件会发生两次,这时只要不引用zepto.js的touch.js就可以了,只用mui的tap事件,如: mui(".infor_header ...
- css引入方式
1.<style> body{} </style> 2.写在一个单独的文件里面保存即新建一个文件:xx.css; 注明该文件的位置<link re ...
- iptables的conntrack表满了导致访问网站很慢
iptables的conntrack表满了导致访问网站很慢 转载自:https://my.oschina.net/jean/blog/189935 检查系统conntrack表是否满 现象:突然发现访 ...
- 通过批处理来运行python程序
>准备 >>在所用的python源程序最前面指定该源程序要用那种可执行程序去运行它 >>例如: #!/bin/sh shell脚本 #!/usr/bin/perl per ...
- goim 及时消息 集成
https://github.com/roamdy/goim-oc-sdk goim 及时消息 集成
- 显示python已安装模块及路径,添加修改模块搜索路径
在python交互模式下输入: help('modules') #可以显示出已安装的模块 在python交互模式下输入: import sys sys.path #可以显示出模块搜索路径 增加搜索路径 ...
- Design and Analysis of Algorithms_Decrease-and-Conquer
I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...
- Leetcode: Optimal Account Balancing
A group of friends went on holiday and sometimes lent each other money. For example, Alice paid for ...