Android学习笔记_12_网络通信之从web获取资源数据到Android
2014-02-26 17:06
501 查看
从web获取图片信息,并显示到android的imageView控件。
一、添加网络访问权限。
二、界面布局及activity的实现:
一、添加网络访问权限。
<uses-permission android:name="android.permission.INTERNET"/>
二、界面布局及activity的实现:
<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/btnImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="34dp" android:layout_marginTop="15dp" android:text="查看图片" /> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/btnImage" android:layout_below="@+id/btnImage" android:layout_marginTop="20dp" /> </RelativeLayout>
package com.example.image; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { private Button button = null; private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) this.findViewById(R.id.btnImage); imageView = (ImageView) this.findViewById(R.id.imageView); final String path="http://192.168.8.103:8080/Simple/0001.jpg"; button.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { try { byte[] data = getImage(path); Bitmap bm=BitmapFactory.decodeByteArray(data, 0, data.length); imageView.setImageBitmap(bm); } catch (Exception e) { e.printStackTrace(); } } }); } /** * 获取图片流 * @param path * @return * @throws Exception */ public static byte[] getImage(String path) throws Exception{ //创建URL对象,获取HttpURLConnection HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); if (conn.getResponseCode()==200) { //处理从web返回的资源数据 return read(conn.getInputStream()); } return null; } /** * 将流转化成字节数组 * @param in * @return * @throws Exception */ public static byte[] read(InputStream in) throws Exception{ ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int len=0; while((len=in.read(buf))!=-1){ outputStream.write(buf,0,len); } outputStream.close(); return outputStream.toByteArray(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
相关文章推荐
- Android学习笔记_12_网络通信之从web获取资源数据到Android
- Android开发18——获取网络资源之json数据
- Android核心基础-7.Android 网络通信-1.获取文本数据
- Android开发18——获取网络资源之json数据
- Android网络通信:通过volley和gson获取聚合数据
- Android开发17——获取网络资源之XML数据
- Android使用AsyncTask异步线程网络通信获取数据(get json)
- Android开发17——获取网络资源之XML数据
- Android开发17——获取网络资源之XML数据
- Android HttpURLConnection(Get,Post方式)进行网络通信 获取数据和网络图片
- Android开发18——获取网络资源之json数据
- Android进阶2之Https访问网络资源(获取网络图片)
- Android网络开发中如何使用JSON进行网络通信---Android_JSON数据通讯方法解析
- Android获取网络XML/JSON数据
- Android-Volley网络通信框架(二次封装数据请求和图片请求(包括处理请求队列和图片缓存))
- Android核心基础-7.Android 网络通信-4.获取网络XML,JSON
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
- Silverlight 2系列(12):数据与通信之WebClient
- Android网络开发:通过URL获取网络资源
- Android网络编程-----从服务器端获取xml数据并解析