zoukankan      html  css  js  c++  java
  • 07_图片查看器完成

     

     


    如果图片的路径错了,可能会走抛出异常这一条路。那就要弹吐司提醒一下用户,弹吐司也是在修改界面。不能在子线程的异常处理那里弹吐司。走到异常里也会有发消息的情况。如果每一个Message拿过来都给它强转成Bitmap它肯定会挂掉。


     图片的地址写错了,服务器返回404异常,图片是能返回到。如果图片地址是http://100.0.2.2:8080/tomcat1.png那就应该是网络连接问题了。

    通过switch可以区分不同的消息类型,根据不同的消息类型来处理不同的消息。



    package
    com.itheima.picview; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener{ private EditText et_url;//成员变量 private Button btn_show; private ImageView iv_pic; /** * 加上注释:成功获取数据 */ private final int GET_DATA_SUCCESS = 1; private final int NETWORK_ERROR = 2;//如果是2就是走到异常里,网络错误 private final int SERVER_ERROR = 3;//如果是3就是服务器返回错误 //case还可以分的更细一点,如果返回码是404就是服务器路径的问题了.如果返回码是500就是服务器的错误. private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub //super.handleMessage(msg); switch (msg.what) { case GET_DATA_SUCCESS://msg.what=1是成功地拿到图片了 Bitmap bm = (Bitmap) msg.obj; iv_pic.setImageBitmap(bm); break; case NETWORK_ERROR://换这种写法一看这个名字就知道是什么意思了 Toast.makeText(MainActivity.this, "网络连接异常", Toast.LENGTH_SHORT).show(); break; case SERVER_ERROR: Toast.makeText(MainActivity.this, "服务器返回异常", Toast.LENGTH_SHORT).show(); break; default: break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_url = (EditText) findViewById(R.id.et_url); btn_show = (Button) findViewById(R.id.btn_show); iv_pic = (ImageView) findViewById(R.id.iv_pic); et_url.setText("http://10.0.2.2:8080/tomcat1.png"); btn_show.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub new Thread(){ public void run() { //①获取网址 String path = et_url.getText().toString().trim(); //②联网 try { URL url = new URL(path); HttpURLConnection openConnection = (HttpURLConnection) url.openConnection(); //设置请求方式 openConnection.setRequestMethod("GET"); openConnection.setConnectTimeout(10000);//设置连接超时时间是10秒 int responseCode = openConnection.getResponseCode();//获取响应码 if(responseCode==200){ //获取流 InputStream inputStream = openConnection.getInputStream();//拿到输入流 //输入流如果传的是图片的地址,如果传的是一个图片的地址这个流实际上就是一个图片.图片对应的就是一个Bitmap对象 //通过流创建一个bitmap对象 //iv_pic.setImageBitmap(bm);// Bitmap bitmap = BitmapFactory.decodeStream(inputStream);//想创建Bitmap用到的就是BitmapFactory(位图工厂).decodeStream把inputStream给它拿到了 //创建消息 Message msg = Message.obtain(); //通过消息携带bitmap在主线程中展示 msg.what = GET_DATA_SUCCESS;//通过msg.what去区分不同的消息 msg.obj = bitmap;//把bitmap对象给它带过去了 //通过handler发送消息给主线程 handler.sendMessage(msg); }else{ Message msg = Message.obtain(); msg.what = SERVER_ERROR;//what=3意味着它的返回码不是200而是其他的情况 handler.sendMessage(msg); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); Message msg = Message.obtain(); msg.what = NETWORK_ERROR;//what=2意味着获取数据出错了,联网出错了 handler.sendMessage(msg);//再给它发消息Bitmap没获取到 } //③获取图片 //展示图片 }; }.start();//联网要开子线程 } }
    <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" >
    
        <EditText
            android:id="@+id/et_url"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textUri"
            android:hint="请输入网址" />
         <Button 
             android:id="@+id/btn_show"
             android:layout_below="@id/et_url"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="显示图片"/>
    
         <ImageView
             android:id="@+id/iv_pic"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_alignRight="@+id/btn_show"
             android:layout_below="@id/btn_show" />
             
    </RelativeLayout>
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.itheima.picview"
        android:versionCode="1"
        android:versionName="1.0" android:installLocation="preferExternal">
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET"/>
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.itheima.picview.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
  • 相关阅读:
    MoveWindow() SetWindowPos()的区别与联系
    SEO搜索引擎优化基础
    Windows核心编程小结1
    STL学习笔记8 -- 函数对象
    Java关于反射
    多线程处理慢sql查询小笔记~
    前端小菜鸡使用Vue+Element笔记(二)
    前端小菜鸡使用Vue+Element笔记(一)
    Hive/hbase/sqoop的基本使用教程~
    Hive/Hbase/Sqoop的安装教程
  • 原文地址:https://www.cnblogs.com/ZHONGZHENHUA/p/7072364.html
Copyright © 2011-2022 走看看