android 加载网络图片
2016-08-31 16:29
387 查看
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top|center" android:text="加载" /> <ImageView android:id="@+id/imageView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
网络类
package com.example.viewwebimagedemo.utils; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class HttpUtils { private final static String URL_PATH = "http://images.cnblogs.com/cnblogs_com/liuning8023/588559/o_Android.jpg"; public HttpUtils() { // TODO Auto-generated constructor stub } public static InputStream getImageViewInputStream() throws IOException { InputStream inputStream = null; URL url = new URL(URL_PATH); if (url != null) { HttpURLConnection httpURLConnection = (HttpURLConnection) url .openConnection(); httpURLConnection.setConnectTimeout(3000); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setDoInput(true); int response_code = httpURLConnection.getResponseCode(); if (response_code == 200) { inputStream = httpURLConnection.getInputStream(); } } return inputStream; } }
package com.example.viewwebimagedemo; import java.io.InputStream; import com.example.viewwebimagedemo.R; import com.example.viewwebimagedemo.utils.HttpUtils; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private Button button; private ImageView imageView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button) this.findViewById(R.id.btn); imageView = (ImageView) this.findViewById(R.id.imageView); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(access).start(); } }); } // 另一个线程从网络获得图片 private Runnable access = new Runnable() { @Override public void run() { getImg(); } }; // 获得图片 public void getImg() { try { InputStream inputStream = HttpUtils.getImageViewInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); Looper.prepare();// 必须调用此方法,要不然会报错 Message msg = new Message(); msg.what = 0; msg.obj = bitmap; handler.sendMessage(msg); } catch (Exception e) { Toast.makeText(getApplicationContext(), "获取图片错误", 1).show(); } } // 更新UI信息 private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 0) { if (msg.obj == null) { Toast.makeText(getApplicationContext(), "图片不存在", 1).show(); } else { Toast.makeText(getApplicationContext(), "加载图片...", 1) .show(); setImg((Bitmap) msg.obj); } } } }; // 加载图片 private void setImg(Bitmap bm) { imageView.setImageBitmap(bm); } }
效果:
记得加入权限: <uses-permission android:name="android.permission.INTERNET" />
相关文章推荐
- android listview 中从网络加载图片
- Android异步加载网络图片
- Android网络图片异步加载
- Android加载网络图片学习过程
- Android异步网络加载图片的控件
- android中listView实现异步加载网络图片
- Android在网络异步加载图片方式总结
- [Android] AsyncTask使用实例---加载网络图片
- android中listView实现异步加载网络图片
- Android异步加载网络图片-good
- android异步回调加载网络图片
- Android Listview异步动态加载网络图片
- Android从网络上 异步加载图片
- Android ListView异步加载网络图片
- Android中网络图片的异步加载
- android异步回调加载网络图片
- Android之ListView异步加载网络图片(优化缓存机制)
- android从网络获取图片以及异步加载图片
- 【Android】加载网络图片-------转自1
- Android实现异步从网络加载图片列表