本案例使用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显示网络图片的更多相关文章

  1. android用ImageView显示网络图片

    1.权限配置 <</SPAN>uses-permission android:name="android.permission.INTERNET"/> .c ...

  2. 学习Android之SimpleAdapter显示网络图片

    效果图: 此程序基本的知识点是:SimpleAdapter本身是不支持网络图片的, 假设在Map.put(a,b)中 b为一个Bitmap,程序不会报红色字体,而是在控制台输出绿色的字体,例如以下 0 ...

  3. ImageView显示网络图片

    package com.example.urlimage; import java.io.InputStream; import java.net.HttpURLConnection; import ...

  4. Android用ImageView显示本地和网上的图片

    ImageView是Android程序中经常用到的组件,它将一个图片显示到屏幕上. 在UI xml定义一个ImageView如下: public void onCreate(Bundle savedI ...

  5. Android 利用ImageView显示图片

    Author: Maddock Date: 2015-07-21 因为做算法demo的需要,开发一点安卓的程序. 需求:获取UI中图像中某点的坐标. 参考:http://longshuai2007.b ...

  6. Android 本地加载网页与显示网络图片

    有时候需要在应用程序里展示一些网页,但是需求里又明确指出,不允许打开系统浏览器,显然也不可能去编写一个浏览器出来,这时就需要使用 WebView控件,借助它我们就可以在自己的应用程序里嵌入一个浏览器, ...

  7. Android 显示网络图片

    本文内容 环境 演示显示网络图片 本文演示 Android 如何显示网络图片.学习一门新的语言,最好办法就先了解该语言的语法和库,以及设计思想,再着手现实一些常用功能,毕竟以后用该语言是要写程序的,而 ...

  8. Android自己定义圆角ImageView 支持网络图片

    先看下效果图 我们再来看一张CSDN的圆角图片 从布局能够看出csdn app 的头像也是圆角的Image,但能够看到.有明显的毛刺感.不知道是csdn 程序猿的疏忽还是 我手机的问题,本人手机(小米 ...

  9. [Android]异步加载图片,内存缓存,文件缓存,imageview显示图片时增加淡入淡出动画

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3574131.html  这个可以实现ImageView异步加载 ...

随机推荐

  1. [thinkphp] MD!! 数组构造的好好的,硬是有一个值无法写入数据库

    我都要抓狂了,buildsql()方法又用不了,最后决定看runtime里面的文件.先删掉所有的runtime,然后提交一次,就可以在runtime里面看到对应解析后的文件,这样应该可以知道问题在哪. ...

  2. android利用adb修改手机的分辨率和dpi

    在android开发过程中,适配更多的适配是必不可少的一步,而每次测试适配时,要么购买设配,要么模拟器,买设配太花钱,模拟器太占内存,不过幸好还可以通过修改手机的size(分辨率)和density来进 ...

  3. 写给嵌入式程序员的循环冗余校验(CRC)算法入门引导

    写给嵌入式程序员的循环冗余校验(CRC)算法入门引导 http://blog.csdn.net/liyuanbhu/article/details/7882789 前言 CRC校验(循环冗余校验)是数 ...

  4. Visual Studio 行末回车时运算符两侧自动加空格是怎么设置的

    在工具—>选项->文本编辑器->c#->格式设置->间距后有三个选项 如果已经选中"二元运算符前后插入空格" 而VS又抽风没有这个功能时,可以选中其余 ...

  5. 咏南下拉列表非数据敏感控件--TYNSearch

    咏南下拉列表非数据敏感控件--TYNSearch 拥有下拉列表控件可以大大地加速软件系统的开发. 控件适用于DELPHI5及以上版本的安装和使用. 控件的使用方法: procedure Tflog.s ...

  6. mysql 的常用查询

    Ø 基本常用查询 --select select * from student;   --all 查询所有 select all sex from student;   --distinct 过滤重复 ...

  7. 常用函数 __MySQL必知必会

    ----------------------使用数据处理函数 ---------------------- 常见的文本处理函数 Left() 返回串左边的字符Length() 返回串的长度Locate ...

  8. JavaScript入门:006—JS函数的定义

    JS函数的声明. 声明函数的格式例如以下: function 函数名(參数列表){ //函数语句: return 返回值; } 来看详细的函数声明.1.普通函数 <script type=&qu ...

  9. ES6里关于数字的拓展

    一.指数运算符 ES6引入的唯一一个JS语法变化是求幂运算符,它是一种将指数应用于基数的数学运算.JS已有的Math.pow()方法可以执行求幂运算,但它也是为数不多的需要通过方法而不是正式的运算符来 ...

  10. 常见java异常

    1. java.lang.NullPointerException(空指针异常)  调用了未经初始化的对象或者是不存在的对象 经常出现在创建图片,调用数组这些操作中,比如图片未经初始化,或者图片创建时 ...