80,351
社区成员
发帖
与我相关
我的任务
分享
package com.zjc.zjchomework;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.zjc.zjchomework.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
public class ImageViewActivity extends Activity
{
public String urlString;
ListView mListView = null;
private ImageView imageIV;
private Handler handler=new Handler(); //在主线程中创建handler
private ImageService service;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.imageview_);
Intent intent=getIntent();
//打开“邮件”
//Bundle urlBundle = intent.getBundleExtra("urlAdress");
//获得网络图片地址
urlString = "http://m4.auto.itc.cn/car/120/95/90/Img1579095_120.jpg";//urlBundle.getString("url");
imageIV = (ImageView) this.findViewById(R.id.imageView1);
ShowImageFromURL("http://m4.auto.itc.cn/car/120/95/90/Img1579095_120.jpg");
}
public void ShowImageFromURL(final String url)
{
//把图片放在一个新的线程里面来读取.
new Thread()
{//创建一个新的线程
public void run()
{
try {
String address = "http://m4.auto.itc.cn/car/120/95/90/Img1579095_120.jpg";// addressET.getText().toString();
//由于网络操作比较耗时,所以在新线程中操作
final Bitmap image = service.getImage(address);
handler.post(new Runnable()
{
public void run()
{
imageIV.setImageBitmap(image);//新线程更新界面,需要使用handler
}
});
}
catch (Exception e)
{
e.printStackTrace();
Toast.makeText(getApplicationContext(), "服务器忙,请稍后再试!", 0).show();
}
}
}.start();
}
}
/*
*
* */