显示网络中的图片
来源:互联网 发布:数据有效性检验 挖掘 编辑:程序博客网 时间:2024/05/16 17:12
丶Android中显示网络上的图片
最终效果图显示如下:
MainActivity.java主要代码展示:
package com.example.imageviews;import java.io.IOException;private Handler handler=new Handler(){ public void handleMessage(android.os.Message msg){ switch (msg.what) { case 1: Bitmap bitmap=(Bitmap) msg.obj; ivName.setImageBitmap(bitmap); break; default: break; } } }; public void viewImage(View view){ final String path=etUrl.getText().toString(); if(TextUtils.isEmpty(path)){ Toast.makeText(this, "内容为空", Toast.LENGTH_LONG).show(); }else{ new Thread(){ public void run(){ try { //链接服务器,获取图片 URL url=new URL(path); HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setReadTimeout(5000); //设置链接超时时间 int op=httpURLConnection.getResponseCode(); if(op==200){ InputStream in=url.openStream(); Bitmap bitmap=BitmapFactory.decodeStream(in); Message msg=new Message(); msg.what=1; msg.obj=bitmap; handler.sendMessage(msg); } else{ Toast.makeText(MainActivity.this, "图片获取失败", Toast.LENGTH_LONG).show(); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }.start(); } }
activity_main.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" > <ImageView android:id="@+id/iglook" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="72dp" android:src="@drawable/ddd" /> <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="31dp" android:text="浏览" /> <EditText android:id="@+id/etext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button1" android:layout_centerHorizontal="true" android:layout_marginBottom="35dp" android:ems="10" android:hint="请输入图片的路径" > <requestFocus /> </EditText></RelativeLayout>
0 0
- 显示网络中的图片
- Android显示网络中的图片
- listview中的imageview获取网络图片重复显示的问题
- android--(http协议、显示网络中的图片、https)
- WinForm显示网络图片
- GridView显示网络图片
- Android 网络图片显示
- ImageView显示网络图片
- SimpleAdapter显示网络图片
- C#显示网络图片
- Android显示网络图片
- listview显示网络图片
- 加载显示网络图片
- 显示网络图片
- Imageview显示网络图片
- qml---------------显示网络图片
- listview显示网络图片
- Qt显示网络图片
- JSON 之JAVA 解析
- 友元函数、类和运算符重载
- OC基础4:类和方法
- WWDC 2012 Session笔记——219 Advanced Collection Views and Building Custom Layouts
- Android版本与过高的JDK版本不兼容问题
- 显示网络中的图片
- 从今天开始正式向软件开发进军
- 备份及恢复votedisk
- JAVA之面向对象编程学习
- PHP网页下的注入原理
- AnimationCurve
- Android的原始资源Raw和Assert资源的使用-android学习之旅(五十七)
- [转载]socket模型
- iOS and OS X Network Programming Cookbook |Creating an echo Client(Bonjour)