Android中显示网络图片

本文参照自:  http://developer.51cto.com/art/201001/180968.htm
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
 
   

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" >
   
        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"
  />

另外在AndroidManifest.xml中需要添加以下权限,以便访问网络
 
   

android:name="android.permission.INTERNET" />

结束!

你可能感兴趣的:(问题小汇)