网络图片浏览器——HttpURLConnection
来源:互联网 发布:js 添加dom 动画 编辑:程序博客网 时间:2024/06/05 09:38
这是我们今天要做的案例的运行效果图:
在这个案例中运用到了线程,Handler,HttpURLConnection几个知识点。
1.布局界面
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="cn.edu.bzu.httppicture.MainActivity"> <ImageView android:id="@+id/iv_image" android:layout_weight="1000" android:layout_width="match_parent" android:layout_height="match_parent" /> <EditText android:id="@+id/et_path" android:layout_width="match_parent" android:layout_height="wrap_content" android:lines="1" android:text="http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="click" android:text="浏览" /></LinearLayout>2.MainActivity
public class MainActivity extends AppCompatActivity { private EditText et_path; private ImageView iv_image; protected static final int UPDATA = 1; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case UPDATA: Bitmap bitmap = (Bitmap) msg.obj; iv_image.setImageBitmap(bitmap); break; default: break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_path = (EditText) findViewById(R.id.et_path); iv_image = (ImageView) findViewById(R.id.iv_image); } public void click(View view) { final String path = et_path.getText().toString().trim(); if (TextUtils.isEmpty(path)) { Toast.makeText(this, "图片路径不能为空", Toast.LENGTH_SHORT).show(); } else { new Thread() { @Override public void run() { try { URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); int code = connection.getResponseCode(); if (code == 200) { InputStream is = connection.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(is); Message message = new Message(); message.what = UPDATA; message.obj = bitmap; handler.sendMessage(message); } else { Toast.makeText(MainActivity.this, "图片错误", Toast.LENGTH_SHORT).show(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }; }.start(); } }}3.清单文件
添加:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>允许请求网络。
阅读全文
0 0
- 网络图片浏览器——HttpURLConnection
- 使用HttpURLConnection访问网络——加载网络图片
- Android网络图片处理——Picasso与HttpUrlConnection比较
- HttpURLConnection下载网络图片
- HttpURLConnection获取网络图片
- HttpURLConnection下载网络图片
- 网络连接——HttpURLConnection
- 网络访问——HttpURLConnection
- Android HttpURLConnection 加载网络图片
- android网络HttpURLConnection抓取网络图片
- android网络应用3——HttpURLConnection
- HTTP协议访问网络——HttpURLConnection
- Android 网络通信——HttpURLConnection
- android学习——网络访问HttpURLConnection
- Android——网络通信(HttpUrlConnection)
- Android中网络请求—HttpClient、HttpURLConnection
- 安卓案例——网络图片浏览器
- 使用HttpURLConnection从网络中下载图片
- 51nod 1202 子序列个数
- c++实验6-矩阵求和
- HTML5废弃的iframe有哪些缺点?
- drawble 的使用
- android常见三个适配器
- 网络图片浏览器——HttpURLConnection
- BeanShell 简介与学习(一篇就够)
- 比较版本号的大小 新旧
- 捕获程序崩溃异常
- 计算机与操作系统基础-计算机发展史
- 双线性插值
- 项目风险管理脉络
- Nginx 服务器基础配置指令
- <%@include file="/1.jsp"%> 标签使用注意事项