利用Http从指定地址下载图片并显示出来
/** * 简单的封装一个http下载类 * */ public class HttpUtils { //指定一张图片 private String URL_PATH = "http://img.wangxiao.cn/files/RemoteFiles/20140420/095185001.jpg"; public HttpUtils(){ } //返回一个byte类型的数组 public byte[] getData(){ HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(URL_PATH); HttpResponse httpResponse = null; try { httpResponse = httpClient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode() == 200) { byte[] data = EntityUtils.toByteArray(httpResponse .getEntity()); return data; } } catch (Exception e) { e.printStackTrace(); } return null; } }
//用于调用的avtivity public class MainActivity extends Activity { private Button btnDown; private ImageView img; private ProgressDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnDown = (Button) findViewById(R.id.button1); img = (ImageView) findViewById(R.id.p_w_picpathView1); dialog = new ProgressDialog(this); dialog.setTitle("提示信息"); dialog.setMessage("正在下载中,请稍后..."); dialog.setCancelable(false); btnDown.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(mRunnable).start(); dialog.show(); } }); } private Runnable mRunnable = new Runnable() { @Override public void run() { // TODO Auto-generated method stub byte[] data = new HttpUtils().getData(); Message msg = Message.obtain(); msg.obj = data; msg.what = 0x001; handler.sendMessage(msg); } }; private Handler handler = new Handler() { // 在Handler中获取消息,重写handleMessage()方法 @Override public void handleMessage(Message msg) { // 判断消息码是否为0x001 if(msg.what == 0x001){ byte[] data=(byte[])msg.obj; Bitmap bmp=BitmapFactory.decodeByteArray(data, 0, data.length); img.setImageBitmap(bmp); dialog.dismiss(); } } }; }
//布局 .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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="54dp" android:text="下载" /> <ImageView android:id="@+id/p_w_picpathView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="79dp" android:src="@drawable/ic_launcher" /> </RelativeLayout>
//最后manifest中别忘了 网络权限 <uses-permission android:name="android.permission.INTERNET" />
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。