实验 图片查看器
来源:互联网 发布:seo网站编辑招聘 编辑:程序博客网 时间:2024/05/16 09:48
一 实验现象
二 实验部分代码
package com.example.getpic;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import javax.net.ssl.HttpsURLConnection;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.ImageView;public class MainActivity extends Activity{ private EditText et_path; private ImageView iv; private Button bt; private Handler handler = new Handler(){public void handleMessage(android.os.Message msg){Bitmap bitmap = (Bitmap) msg.obj;iv.setImageBitmap(bitmap);} };@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);et_path=(EditText) findViewById(R.id.et_path);iv = (ImageView) findViewById(R.id.iv);bt=(Button) findViewById(R.id.button1);}public void click(View v){new Thread(){public void run(){try{String path = et_path.getText().toString().trim(); URL url = new URL(path);HttpURLConnection httpURLConnection = (HttpsURLConnection) url.openConnection();httpURLConnection.setRequestMethod("GET");httpURLConnection.setConnectTimeout(5000);int responsecode=httpURLConnection.getResponseCode();if(responsecode==200){InputStream is =httpURLConnection.getInputStream();Bitmap bitmap =BitmapFactory.decodeStream(is);Message msg = new Message();msg.obj=bitmap;handler.sendMessage(msg);}}catch(Exception e){e.printStackTrace();}}}.start();}@Overridepublic 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;}}
界面设计
<ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="78dp" android:src="@drawable/ic_launcher" /> <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="25dp" android:onClick="click" android:text="查看" /> <EditText android:id="@+id/et_path" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button1" android:layout_centerHorizontal="true" android:ems="10" android:text="https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png" />
三 实验后体会
首先不能忘记添加权限:android.permission.INTERNET
由于android下的网络连接不能在主线程操作,另外UI的改变也只能通过主线程来变化,所以说操作网络要开启一个子线程执行,而UI的改变则通过handler消息传递给主线程,委托主线程操作。
0 0
- 实验 图片查看器
- 实验_图片查看器
- Android实验八之图片查看器
- 实验十 网络图片查看器
- android实验二 图片查看器的设计与实现
- 课程实验,实现网络图片的查看!
- 调用系统图片查看器查看图片
- bootstrap+swiper 图片查看器 图片查看
- Android 实验——实现查看网络图片功能
- C#图片查看器
- C#图片查看器
- 图片查看器beta1
- Android图片查看器
- 图片查看器
- 图片查看器
- matlab图片查看器
- Android 图片查看器
- 模拟图片查看器
- MySQL 免装版配置(服务无法启动)
- JSON.parse()和JSON.stringify()
- 数据结构实验之查找四:二分查找
- 第十四周11
- spark internal - 作业调度
- 实验 图片查看器
- Token
- MAC版MySQL安装完成后初始密码修改
- [vijos1081]野生动物园(splay)
- apache 日志中记录代理IP以及真实客户端IP
- elf可执行文件的理解(附上elf文件格式图解)
- JDBC的基本操作(CRUD)
- CSS画图形
- (最新教程)基于Windows7+visual Studio2013+Python2.7.12环境下的Caffe配置学习