Android使用ImageView显示网络图片
本案例使用ImageView 简单的实现了网络图片的调用。当中注意事项。由于用到了网络,这里採用了HttpClient方法訪问网络联接,关于怎样使用,可參照文章 Android中使用HttpClient实现HTTP通信效果 ,因此。须要注意配置网络权限问题。以及须要使用新线程及Handler来更新Activity,不然会直接报错Not Main Thread
看实例:
MainActivity.java
package com.example.imageview; import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.graphics.Bitmap;
import android.view.Menu;
import android.widget.ImageView; public class MainActivity extends Activity {
private Bitmap bm = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); final Handler handler = new Handler();
new Thread() {
public void run() {
bm = new ApacheHttpClient()
.getHttpBmp("http://www.qilujiaju.com/data/attachment/block/c9/c960ba426890a8ddbfc35d2b4b0d97c9.jpg");
handler.post(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageBitmap(bm);
}
});
}
}.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.activity_main, menu);
return true;
} }
ApacheHttpClient.java
package com.example.imageview; import java.io.IOException;
import java.io.InputStream; import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient; import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log; public class ApacheHttpClient {
private static final String TAG = "Error"; public InputStream httpGet(String url) {
InputStream result = null;
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpGet);
int httpStatus = httpResponse.getStatusLine().getStatusCode();
if (httpStatus == HttpStatus.SC_OK) {
InputStream in = httpResponse.getEntity().getContent();
try {
result = in;
} catch (Exception e) {
Log.i(TAG, "Exception");
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
result = null;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i(TAG, "ClientProtocolException");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i(TAG, "ClientProtocolException");
}
return result;
} public Bitmap getHttpBmp(String url) {
Bitmap bm = null;
InputStream is = httpGet(url);
bm = BitmapFactory.decodeStream(is);
return bm;
}
}
AndroidMainFest.xml
<? xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.imageview"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" /> <uses-permission android:name="android.permission.INTERNET" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.imageview.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>
activity_main.xml
<RelativeLayout 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"
tools:context=".MainActivity" > <ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/app" /> </RelativeLayout>
这是一个完整的实例,可直接执行于模拟器或真机。
Android使用ImageView显示网络图片的更多相关文章
- android用ImageView显示网络图片
1.权限配置 <</SPAN>uses-permission android:name="android.permission.INTERNET"/> .c ...
- 学习Android之SimpleAdapter显示网络图片
效果图: 此程序基本的知识点是:SimpleAdapter本身是不支持网络图片的, 假设在Map.put(a,b)中 b为一个Bitmap,程序不会报红色字体,而是在控制台输出绿色的字体,例如以下 0 ...
- ImageView显示网络图片
package com.example.urlimage; import java.io.InputStream; import java.net.HttpURLConnection; import ...
- Android用ImageView显示本地和网上的图片
ImageView是Android程序中经常用到的组件,它将一个图片显示到屏幕上. 在UI xml定义一个ImageView如下: public void onCreate(Bundle savedI ...
- Android 利用ImageView显示图片
Author: Maddock Date: 2015-07-21 因为做算法demo的需要,开发一点安卓的程序. 需求:获取UI中图像中某点的坐标. 参考:http://longshuai2007.b ...
- Android 本地加载网页与显示网络图片
有时候需要在应用程序里展示一些网页,但是需求里又明确指出,不允许打开系统浏览器,显然也不可能去编写一个浏览器出来,这时就需要使用 WebView控件,借助它我们就可以在自己的应用程序里嵌入一个浏览器, ...
- Android 显示网络图片
本文内容 环境 演示显示网络图片 本文演示 Android 如何显示网络图片.学习一门新的语言,最好办法就先了解该语言的语法和库,以及设计思想,再着手现实一些常用功能,毕竟以后用该语言是要写程序的,而 ...
- Android自己定义圆角ImageView 支持网络图片
先看下效果图 我们再来看一张CSDN的圆角图片 从布局能够看出csdn app 的头像也是圆角的Image,但能够看到.有明显的毛刺感.不知道是csdn 程序猿的疏忽还是 我手机的问题,本人手机(小米 ...
- [Android]异步加载图片,内存缓存,文件缓存,imageview显示图片时增加淡入淡出动画
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3574131.html 这个可以实现ImageView异步加载 ...
随机推荐
- EntityFramework之一对多关系(三)
上篇介绍了一对一关系,下面介绍下一对多关系代码编写. 1.新建model实体,Product是产品类,Order是订单,一个产品对应多个订单 public class Product { public ...
- 状压DP【p1896】[SCOI2005]互不侵犯
Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. Input 只有一行,包 ...
- POJ 1741 Tree 树的分治
原题链接:http://poj.org/problem?id=1741 题意: 给你棵树,询问有多少点对,使得这条路径上的权值和小于K 题解: 就..大约就是树的分治 代码: #include< ...
- 【bzoj1123】【[POI2008]BLO】tarjan判割点
(上不了p站我要死了,侵权度娘背锅) Description Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 所有t ...
- elasticsearch5.3.0 安装
公司有项目打算用elasticsearch,所以研究了下,目前最新版本5.3.0 安装 1.下载包 https://artifacts.elastic.co/downloads/elasticsea ...
- EditText中输入信息的限制的方法
应用场景 在Android应用中有时需要EditText中只允许输入约定的一些字符,禁止输入其他字符.这里列举了一些可能的应用场景. 1. 场景一 在通讯录保存好友信息界面中填写好友的电话号码时,应当 ...
- ckeditor的详细配置(转)
CKEditor 3 JavaScript API Documentation : http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.con ...
- python数据类型学习心得
python中的数据类型 数字:整型,长整形,布尔型,浮点型,复数 整型:普通的整数,在32位的操作系统中范围在-2的-32次方到2的32次方-1,64位的操作系统则为-2的64次方到2的64次方-1 ...
- ISP模块之RAW DATA去噪(一)
ISP(Image Signal Processor),图像信号处理器,主要用来对前端图像传感器输出信号处理的单元,主要用于手机,监控摄像头等设备上. RAW DATA,可以理解为:RAW图像就是CM ...
- 【AS3 Coder】任务五:Flash 2D游戏的第二春(下)
在上一节中,我们基本上已经讲完了游戏中最主要的逻辑部分,不过为了更加全面地运用Starling中的一些特性,在本节中我们将一起来看看如何实现多面板切换以及粒子效果,这两个玩意儿可是比较频繁会出现于St ...