查看网络图片功能的应用程序
2015-06-27 19:07
483 查看
使用Android线程机制来实现这个功能
效果图:
当在EditText中输入地址后,点击浏览按钮,会在EditText上面空白地方显示该地址对应的一张图片。
activity_main布局代码:
MainActivity代码:
注意:UI线程只允许修改Activity里的UI组件,然后在androidManifest中必须有配置文件。注意开始线程的使用!!
效果图:
当在EditText中输入地址后,点击浏览按钮,会在EditText上面空白地方显示该地址对应的一张图片。
activity_main布局代码:
<LinearLayout 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:orientation="vertical" 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/ivImage" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:layout_weight="1" /> <EditText android:id="@+id/etImageUrl" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="请输入图片的地址" > <requestFocus /> </EditText> <Button android:id="@+id/btnView" android:layout_width="278dp" android:layout_height="wrap_content" android:background="@drawable/back_button" android:onClick="showImage" android:text="浏览" /> </LinearLayout>
MainActivity代码:
public class MainActivity extends Activity { Button button; ImageView imageView; EditText ed; private static final int MSG_SUCCESS = 0;// 获取图片成功的标识 private static final int MSG_FAILURE = 1;// 获取图片失败的标识 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findView(); } public void findView() { button = (Button) findViewById(R.id.btnView); imageView = (ImageView) findViewById(R.id.ivImage); ed = (EditText) findViewById(R.id.edt); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } private Handler handler = new Handler(){ public void handleMessage(Message msg) { switch (msg.what) { case MSG_SUCCESS: Bitmap bit = (Bitmap) msg.obj; imageView.setImageBitmap(bit); break; default: break; } }; }; public void viewImage(View view) { final String path = ed.getText().toString();//获取输入的图片路径 if (TextUtils.isEmpty(path)) { Toast.makeText(MainActivity.this, " 路径不为空", Toast.LENGTH_SHORT) .show(); } else { new Thread() { public void run() { URL url; try { url = new URL(path);//发出请求 HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setRequestMethod("GET");//设置请求方式这里的方式必须为大写 connection.setConnectTimeout(5000);//设置超时的时间 int code = connection.getResponseCode();//获得状态码 if (code == 200) { InputStream is = connection.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(is);//写入一个bitmap流 Message m = new Message(); m.what = MSG_SUCCESS; m.obj = bitmap; handler.sendMessage(m); } else { Toast.makeText(MainActivity.this, "打开失败", Toast.LENGTH_SHORT).show(); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }.start(); //必须加start()方法 82 } } }
注意:UI线程只允许修改Activity里的UI组件,然后在androidManifest中必须有配置文件。注意开始线程的使用!!
相关文章推荐
- android 从网络上获取图片
- iOS:关于获取网络类型和运营商信息
- python单线程网络爬虫
- 网络盲写点网络(2015某市海康技术支持笔试)
- android打包准备:混淆第三方jar包(Gson, greenDao,sharesdk,UIL,高德地图, unity,pinyin4j,async http,JPush,EventBus等)
- HTTP协议
- android判断当前网络状态及跳转到设置界面
- 通过NFS启动自制的网络文件系统遇到的问题总结
- [iOS AFNetworking框架实现HTTP请求、多文件图片上传下载]
- 网络编程
- 基于UDP的网络编程
- HttpClient-调用webservice客户端
- 从Jetty、Tomcat和Mina中提炼NIO构架网络服务器的经典模式
- 从Jetty、Tomcat和Mina中提炼NIO构架网络服务器的经典模式
- 【费用流】【网络流24题】【cogs 739】运输问题
- OSPF网络类型详解
- cocos2dx-3.3 网络编程(CURL+PHP) NO.2 登陆功能
- Hadoop2.6集群环境虚拟机中搭建网络配置
- Windows服务器配置与管理-------DHCP服务器搭建与管理
- cocos2dx-3.3 网络编程(CURL+PHP) NO.1 写好PHP代码