本文主要是介绍Android网络通信之网络图片查看器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
转载
一个简单的小应用——网络图片查看器。
根据应用需求,先创建一个web应用,向其中放入一张图片之后部署在Tomcat上。
通过EditText显示网络图片路径,Button控制图片显示,ImageView显示图片。
界面部分代码:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- tools:context=".ImageLookerActivity" >
- <!-- 输入框 -->
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/imagePath"
- android:text="http://172.16.59.35:8080/lss/images/blackboard.jpg"
- />
- <!-- 按钮 -->
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/button"
- android:text="@string/button"
- />
- <!-- 显示图片组件 -->
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/imageView"
- />
- </LinearLayout>
界面效果:
Activity部分代码:
- public class MainActivity extends Activity {
- EditText text;
- ImageView image;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- text=(EditText)this.findViewById(R.id.imagePath);
- image=(ImageView)this.findViewById(R.id.imageView);
- Button button=(Button)this.findViewById(R.id.button);
- button.setOnClickListener(new ButtonClickListener());
- }
- //定义一个按钮监听事件内部类
- private final class ButtonClickListener implements OnClickListener
- {
- public void onClick(View v) {
- //点击按钮,得到文本框中的图片路径
- String path=text.getText().toString();
- byte[] data;
- try {
- //定义一个ImageService业务类,以字节数组的形式得到图片数据
- data = ImageService.getImage(path);
- //使用数组数据生成位图的对象
- Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);
- //将位图放入图片控件
- image.setImageBitmap(bitmap);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- Toast.makeText(getApplicationContext(), R.string.error, 1).show();
- }
- }
- }
- }
ImageService业务类
- public class ImageService {
- /**
- * 获取网络图片的数据
- */
- public static byte[] getImage(String path) throws IOException {
- URL url=new URL(path);
- //得到一个基于http协议的链接对象
- HttpURLConnection conn=(HttpURLConnection)url.openConnection();
- //设置链接的超时时间
- conn.setConnectTimeout(5000);
- conn.setRequestMethod("GET");
- //取得服务器返回的相应码
- if(conn.getResponseCode()==200)
- { InputStream inputStream=conn.getInputStream();
- //定义一个流工具类,将输入流中的内容以字节形式返回
- return StreamTool.read(inputStream);
- }
- return null;
- }
- }
StreamTool工具类
- public class StreamTool {
- /**
- * 读取流中的数据
- */
- public static byte[] read(InputStream inputStream) throws IOException {
- byte[] buffer=new byte[1024];
- ByteArrayOutputStream outStream=new ByteArrayOutputStream();
- int len=0;
- while((len=inputStream.read(buffer))!=-1)
- {
- outStream.write(buffer, 0, buffer.length);
- }
- //关闭输入流
- inputStream.close();
- //返回内存中数据
- return outStream.toByteArray();
- }
- }
注意申请网络访问权限:
- <!-- 允许网络访问 -->
- lt;uses-permission android:name="android.permission.INTERNET"/>
步骤总结:
1、首先得到输入流
InputStream inputStream=conn.getInputStream();
2、根据资源类型转换成对应数据。
Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);
运行结果:
这篇关于Android网络通信之网络图片查看器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!