Android中显示网络图片

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 ;
@Override
public 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 () {
@Override
public void loaded ( Bitmap bitMap , String url ) {
// TODO Auto-generated method stub
if (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 block
e.printStackTrace();
}
conn=null;
}
if(is!=null)
{
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.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" >
    <ImageView
        android: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中需要添加以
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值