Android实验八之图片查看器

    xiaoxiao2021-12-14  20

    实验效果图:

    代码:

    mainactivity.java

    public class MainActivity extends Activity { private EditText et_path; private ImageView iv; private Button button; private Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { Bitmap bitmap = (Bitmap) msg.obj; iv.setImageBitmap(bitmap); }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); et_path = (EditText) findViewById(R.id.et_path); iv = (ImageView) findViewById(R.id.iv); } public void click(View v) { // TODO Auto-generated method stub new Thread(){public void run() { try { //访问图片的路径 String path = et_path.getText().toString().trim(); //创建URL对象 URL url = new URL(path); //获取httpurlconnection HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //设置请求方式 conn.setRequestMethod("GET"); //设置超时时间 conn.setConnectTimeout(8000); //获取服务器返回的状态码 int code = conn.getResponseCode(); if(code == 200){ // 获取图片的数据 不管是什么数据 都是以流的形式返回 InputStream in = conn.getInputStream(); //通过位图工厂获取bitmap Bitmap bitmap = BitmapFactory.decodeStream(in); //把bitmap显示到iv上 Message msg = Message.obtain(); msg.obj = bitmap; handler.sendMessage(msg);//发消息 } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } };}.start(); } @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; } }

    main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/et_path" android:layout_height="wrap_content" android:layout_width="match_parent" android:hint="请输入图片的路径" android:text="https://www.baidu.com/img/bd_logo1.png"/> <Button android:id="@+id/button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onClick="click" android:text="浏览"/> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>

    androidManifest.xml

    <uses-permission android:name="android.permission.INTERNET"/> //获得相应的权限

     

    总结:

    不管什么手机,只要做耗时的操作(比如连接网络,比如拷贝大的数据 等等)就自己开一个子线程,获取数据后想要更新UI,就使用Handler就可以了。

     

    遇到问题:

    点击Button事件的三种调用方法:

    (1)直接调用setonclicklistener方法

    button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub } });

    (2)使用接口

    Mainactivity implements onclicklistener buttton.setonclicklistener(this); public void onclick() { // TODO Auto-generated method stub }

    (3)使用Onclick属性

    <Button android:id="@+id/button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onClick="click" android:text="浏览"/>public void click(View v) { }

     

    转载请注明原文地址: https://ju.6miu.com/read-971585.html

    最新回复(0)