Android中显示网络图片
来源:互联网 发布:海南七星彩缩水软件 编辑:程序博客网 时间:2024/06/01 08:34
在Android中,显示网络图片还是比较简单的。当我们开始启动一个任务加载一个View对应的图片时,应该通过setTag()把该View的tag设置为图片的URL.当实际取得该图片时,应该检查其对应的URL是否和View的TAG一致,只有一致时才把该图片实际应用于该View.
实例1
InternetImageDemoActivity.java文件
package com.lenovo.robin.test;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.AsyncTask;import android.os.Bundle;import android.widget.ImageView;public class InternetImageDemoActivity extends Activity {Bitmap bmImg;ImageView imView;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.internet_image_demo);imView = (ImageView) findViewById(R.id.imageView1);String imageUrl = "http://img6.ph.126.net/hBiG96B8egigBULxUWcOpA==/109212290980771276.jpg";final ImageView imageView = imView;imageView.setTag(imageUrl);ImageLoadedCallback callback = new ImageLoadedCallback() {@Overridepublic void loaded(Bitmap bitMap, String url) {// TODO Auto-generated method stubif (url.equals(imageView.getTag())) {imageView.setImageBitmap(bitMap);}}};new DownloadImageTask(callback).execute(imageUrl);}}
class DownloadImageTask extends AsyncTask<String, Integer, Bitmap> {ImageLoadedCallback callback = null;String url = null;DownloadImageTask(ImageLoadedCallback callback) {this.callback = callback;}protected Bitmap doInBackground(String... urls) {URL myFileUrl = null;Bitmap bitmap = null;url = urls[0];try {myFileUrl = new URL(url);} catch (MalformedURLException e) {e.printStackTrace();}HttpURLConnection conn=null;InputStream is=null;try {conn = (HttpURLConnection) myFileUrl.openConnection();conn.setDoInput(true);conn.connect();is = conn.getInputStream();bitmap = BitmapFactory.decodeStream(is);is.close();} catch (IOException e) {e.printStackTrace();}finally{if(conn!=null){try {conn.connect();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}conn=null;}if(is!=null){try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}is=null;}}return bitmap;}protected void onPostExecute(Bitmap bitMap) {callback.loaded(bitMap, url);}}
interface ImageLoadedCallback {public void loaded(Bitmap bitMap, String url);}
关于AsyncTask请参照《AsyncTask简介》
布局文件internet_image_demo.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" ><ImageViewandroid:id="@+id/imageView1"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_alignLeft="@+id/button1"android:layout_below="@+id/textView1"android:layout_marginTop="34dp"/></RelativeLayout>
另外在AndroidManifest.xml中需要添加以
- android中显示网络图片
- android中显示网络图片
- Android中显示网络图片
- Android中显示网络图片
- Android中显示网络图片
- Android中显示网络图片
- Android中显示网络上的图片
- Android中显示网络上的图片
- android中Picasso显示网络图片
- Android 网络图片显示
- Android显示网络图片
- android 网络图片显示
- Android基础篇之在ListView中显示网络图片
- android 获取网络图片显示在Gallery中
- android中使用afinal一行代码显示网络图片
- 在android中显示网络图片及查看页面源代码
- android网络下载图片并且显示在图库中
- Android基础篇之在ListView中显示网络图片
- 《基于Linux嵌入式原理与应用开发》_使用ADS创建工程
- 杭电acm分类
- Update Rollup 10 for Microsoft Dynamics CRM 2011
- 利用vmware虚拟机创建一个软盘映像文件
- C++ 字符串UTF8与GBK转化
- Android中显示网络图片
- 使用FOR XML合并多行查询数据到一行
- 安装SLURM
- javadoc注释规范
- php学习 第二十六节
- UIButton上使用UIEdgeInsetsMaketitle跟图片对齐
- Android中判断是否有声音在播放
- ORACLE性能优化31条
- java基础-demo(黑马程序员)