Android开发技术之──获取网络图片
2011-07-05 09:06
656 查看
在开发中,经常需要从服务端获取资源信息,下面我将获取网络图片的步骤及代码展示给大家:
一、初始化信息
//定义ImageView对象ImageView imgView;
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* * (若使用代理上网)解决android模拟器,不能上网的问题 * 1.通知Java您要通过代理进行连接 * 2.指定代理所在的服务器 * 3.指定代理监听的端口 */ System.getProperties().put("proxySet", "true"); System.getProperties().put("proxyHost","192.168.121.32"); System.getProperties().put("proxyPort", "8080"); setContentView(R.layout.main); //通过findViewById获得ImageView对象 imgView = (ImageView)findViewById(R.id.image_view); //显示图片 showImage();} 二、发送图片请求//显示图片方法private void showImage() { imgView.setImageBitmap(null); String urlpath = "http://image3.xxxxxxx.cn/content/catentries/00000000010136/000000000101360962/fullimage/000000000101360962_1f.png"; //根据要下载的图片的地址,获得图片Bitmap信息 Bitmap bitmap = getHttpBitmap(urlpath); //将得到的Bitmap赋给imgView对象 imgView.setImageBitmap(bitmap); } /* * 从服务器取图片 * http://image3.xxxxxx.cn/content/catentries/00000000010136/000000000101360962/fullimage/000000000101360962_1f.png * 参数:String类型 * 返回:Bitmap类型 */ public static Bitmap getHttpBitmap(String urlpath) { Bitmap bitmap = null; try { //生成一个URL对象 URL url = new URL(urlpath); //打开连接 HttpURLConnection conn = (HttpURLConnection)url.openConnection();// conn.setConnectTimeout(6*1000);// conn.setDoInput(true); conn.connect(); //得到数据流 InputStream inputstream = conn.getInputStream(); Log.i("MyTag", "******************InputStream:"+inputstream+"******************"); bitmap = BitmapFactory.decodeStream(inputstream); //关闭输入流 inputstream.close(); //关闭连接 conn.disconnect(); } catch (Exception e) { // TODO: handle exception Log.i("MyTag", "******************************************"); Log.i("MyTag", "error:"+e.toString()); } Log.i("Debug", "bitmap:"+bitmap); return bitmap; }
经过以上步骤,我们将网络上的图片信息展示到前端!
一、初始化信息
//定义ImageView对象ImageView imgView;
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* * (若使用代理上网)解决android模拟器,不能上网的问题 * 1.通知Java您要通过代理进行连接 * 2.指定代理所在的服务器 * 3.指定代理监听的端口 */ System.getProperties().put("proxySet", "true"); System.getProperties().put("proxyHost","192.168.121.32"); System.getProperties().put("proxyPort", "8080"); setContentView(R.layout.main); //通过findViewById获得ImageView对象 imgView = (ImageView)findViewById(R.id.image_view); //显示图片 showImage();} 二、发送图片请求//显示图片方法private void showImage() { imgView.setImageBitmap(null); String urlpath = "http://image3.xxxxxxx.cn/content/catentries/00000000010136/000000000101360962/fullimage/000000000101360962_1f.png"; //根据要下载的图片的地址,获得图片Bitmap信息 Bitmap bitmap = getHttpBitmap(urlpath); //将得到的Bitmap赋给imgView对象 imgView.setImageBitmap(bitmap); } /* * 从服务器取图片 * http://image3.xxxxxx.cn/content/catentries/00000000010136/000000000101360962/fullimage/000000000101360962_1f.png * 参数:String类型 * 返回:Bitmap类型 */ public static Bitmap getHttpBitmap(String urlpath) { Bitmap bitmap = null; try { //生成一个URL对象 URL url = new URL(urlpath); //打开连接 HttpURLConnection conn = (HttpURLConnection)url.openConnection();// conn.setConnectTimeout(6*1000);// conn.setDoInput(true); conn.connect(); //得到数据流 InputStream inputstream = conn.getInputStream(); Log.i("MyTag", "******************InputStream:"+inputstream+"******************"); bitmap = BitmapFactory.decodeStream(inputstream); //关闭输入流 inputstream.close(); //关闭连接 conn.disconnect(); } catch (Exception e) { // TODO: handle exception Log.i("MyTag", "******************************************"); Log.i("MyTag", "error:"+e.toString()); } Log.i("Debug", "bitmap:"+bitmap); return bitmap; }
经过以上步骤,我们将网络上的图片信息展示到前端!
相关文章推荐
- Android开发获取网络图片时提示java.net.unknownhosteception
- android开发——获取网络图片
- Android开发教程:使用http协议获取网络图片
- Android开发技术网络篇之── http协议post请求方式
- Android开发之网络图片获取
- android开发——获取网络图片
- android开发获取网络图片并可拖拽移动
- android开发--获取网络图片,读取进度条
- 【Android开发】范例1-开启新线程获取网络图片并显示到ImageView中
- Android开发之异步获取并下载网络资源-下载图片和下载文本内容
- android开发-通过HttpURLConnection获取网络内容,Bitmap把二进制格式的图片转为位图
- android开发--获取网络图片,读取进度条
- android开发--获取网络图片,读取进度条
- android之获取网络图片
- Android仿人人客户端(v5.7.1)——对从服务器端(网络)获取的图片进行本地双缓存处理(编码实现)
- 【Android应用开发技术:网络通信】网络服务可发现基本原理
- Android开发之网络图片查看方法BitmapFactory.decodeStream()学习01
- [Android] 获取网络图片并显示
- Android使用RecyclerView和Picasso实现大规模浏览网络图片(附带缓存技术)
- Android网络开发之实时获取最新数据