网络图片查看器
2016-12-04 08:33
295 查看
一、运行效果图
二、核心代码
三、运行中遇到的问题
刚开始运行程序,闪退。代码有问题。后来修改了一下程序,运行成功。
二、核心代码
package com.example.imagebrowser import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; 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.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private EditText editText; private ImageView imageView; private static final int SHOW_IMAGE = 1; private Handler 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; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText) findViewById(R.id.editText); imageView = (ImageView) findViewById(R.id.imageView); } public void showImage(View view) { final String path = editText.getText().toString(); if (TextUtils.isEmpty(path)) { Toast.makeText(this, "图片不能为空", Toast.LENGTH_LONG).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); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }; }.start(); } } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" /> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:text="http://pica.nipic.com/2007-11-09/2007119121849495_2.jpg" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:onClick="showImage" android:text="浏览" /> </LinearLayout>
三、运行中遇到的问题
刚开始运行程序,闪退。代码有问题。后来修改了一下程序,运行成功。
相关文章推荐
- Project——从网络获取数据,实现图片查看器
- Android编程之从网络上获取图片查看器和HTML代码的查看器
- android---利用android-async-http开源项目实现网络图片查看器
- Android项目——网络图片查看器
- 网络图片查看器
- Android实现网络图片查看器和网页源码查看器
- Android实现网络图片查看器和网页源码查看器
- Android 网络图片查看器
- Android -- 网络图片查看器,网络html查看器, 消息机制, 消息队列,线程间通讯
- Android网络图片查看器
- 实习日志(11):Android网络通信之网络图片查看器
- Android入门之网络图片查看器
- Android开发入门之网络通信(网络图片查看器)
- 赵雅智_引用开源框架通过AsyncHttpClient实现网络图片查看器
- android下简单的网络图片查看器
- Android实现网络图片查看器和网页源码查看器
- Android学习笔记---23_网络通信之网络图片查看器
- Android 网络图片查看器
- Android实现网络图片查看器和网页源码查看器
- Android网络通信之网络图片查看器