一、任务描述
输入网络图片的地址,点击浏览按钮可以显示网络中的图片。
二、任务实现
1、完成效果
![](https://img-blog.csdn.net/20161204180612839?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
运行程序,在网络上找张图片,拷贝其路径,点击浏览按钮(本次实验中,采用固定地址的方式)
![](https://img-blog.csdn.net/20161204180621084?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
2、实验步骤
①、UI设计:如图界面,ImageView水平居中显示,EditText,Button居中显示
②、访问网络,从网络中获取图片
![](https://img-blog.csdn.net/20161204181115237?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
1 在Android2.2模拟器上运行是否正常?
如果不正常,原因是什么?
2 若在Android2.2上可以运行了,那么在Android4.0以上模拟器上运行能否正常运行呢?
若不正常,报什么错误?
③、原因分析
ANR(Application Not Responding):应用程序无响应,如果应用程序不能响应用户输入
的话,系统会显示ANR。主线程也是UI线程本身就干了很多事情,绘制界面响应事件等。如果里面再直接放入一些耗时的操作,如连接网络进行IO操作,就会阻塞主线程,带来较差的用户体验。
④、修改程序
![](https://img-blog.csdn.net/20161204181658791?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
⑤Android消息处理机制
![](https://img-blog.csdn.net/20161204181931526?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
⑥进一步修改程序
![](https://img-blog.csdn.net/20161204182040855?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20161204182046296?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
3、核心代码
MainActivity.Java
- <span style="color:#ff0000;">@Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- imageview = (ImageView) findViewById(R.id.iv);
- etUrl = (EditText) findViewById(R.id.etPath);
-
- handler = new Handler(){
- public void handleMessage(android.os.Message msg) {
- switch(msg.what){
- case SHOW_IMAGE:
- Bitmap bitmap = (Bitmap) msg.obj;
- imageview.setImageBitmap(bitmap);
- break;
- default:
- break;
- }
-
- };};
- }
-
-
- public void showImage(View view){
- final String path = etUrl.getText().toString();
- if(TextUtils.isEmpty(path)){
- Toast.makeText(this, "图片路径不能为空", Toast.LENGTH_SHORT).show();
- }else{
- new Thread(){
- public void run(){
-
- try
- {
- URL url = new URL(path);
-
- HttpURLConnection httpURLConnection = (HttpURLConnection) 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.what = SHOW_IMAGE;
- msg.obj=bitmap;
- handler.sendMessage(msg);
-
- }else{
- Toast.makeText(MainActivity.this, "图片显示失败", Toast.LENGTH_SHORT).show();
- }
- } catch (MalformedURLException e)
- {
- e.printStackTrace();
- }catch (IOException e) {
-
- e.printStackTrace();
- }
- };
- }.start();
-
- }
- }
-
- </span>
在AndroidManifest.xml中增加访问网络的权限:
- <span style="color:#ff0000;"><manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="cn.bzu.tong.internetimage"
- android:versionCode="1"
- android:versionName="1.0" >
- ...
- <span style="BACKGROUND-COLOR: rgb(255,255,255)"> <uses-permission android:name="android.permission.INTERNET" /></span>
- ...
- </span><pre class="java" name="code"><span style="color:#ff0000;"></manifest>
- </span></pre>
- <pre><span style="color:#ff0000"></span></pre>
- <p><span style="color:#ff0000"></span></p>
- <pre><span style="color:#ff0000"></span></pre>
- <h3><a name="t12"></a><span style="color:#ff0000">4、体会感悟</span></h3>
- <span style="color:#ff0000">要学会Bitmap、handler等的使用,要熟悉使用网络的编程手法。<br>
- </span>
- <pre></pre>
-