Android学习笔记_12_网络通信之从web获取资源数据到Android
从web获取图片信息,并显示到android的imageView控件。
一、添加网络访问权限。
<uses-permission android:name="android.permission.INTERNET"/>
二、界面布局及activity的实现:
<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" > <Button
android:id="@+id/btnImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="34dp"
android:layout_marginTop="15dp"
android:text="查看图片" /> <ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btnImage"
android:layout_below="@+id/btnImage"
android:layout_marginTop="20dp"
/> </RelativeLayout>
package com.example.image; import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL; import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView; public class MainActivity extends Activity {
private Button button = null;
private ImageView imageView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.btnImage);
imageView = (ImageView) this.findViewById(R.id.imageView);
final String path="http://192.168.8.103:8080/Simple/0001.jpg";
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
try {
byte[] data = getImage(path);
Bitmap bm=BitmapFactory.decodeByteArray(data, 0, data.length);
imageView.setImageBitmap(bm);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* 获取图片流
* @param path
* @return
* @throws Exception
*/
public static byte[] getImage(String path) throws Exception{
//创建URL对象,获取HttpURLConnection
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if (conn.getResponseCode()==200) {
//处理从web返回的资源数据
return read(conn.getInputStream());
}
return null;
}
/**
* 将流转化成字节数组
* @param in
* @return
* @throws Exception
*/
public static byte[] read(InputStream in) throws Exception{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len=0;
while((len=in.read(buf))!=-1){
outputStream.write(buf,0,len);
}
outputStream.close();
return outputStream.toByteArray();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
Android学习笔记_12_网络通信之从web获取资源数据到Android的更多相关文章
- Android学习笔记_13_网络通信之多个上传文件
一.获取HTTP协议: 建立一个Web项目,建立一个如下所示的jsp界面,用IE捕获表单提交信息. <%@ page language="java" contentType= ...
- Android学习笔记_15_网络通信之文件断点下载
一.断点下载原理: 使用多线程下载文件可以更快完成文件的下载,多线程下载文件之所以快,是因为其抢占的服务器资源多.如:假设服务器同时最多服务100个用户,在服务器中一条线程对应一个用户,100条线程在 ...
- Android学习笔记之短信验证码的获取和读取
PS:最近很多事情都拖拖拉拉的..都什么办事效率啊!!! 还得吐槽一下移动运营商,验证码超过五次的时候,直接把我的手机号封闭.真是受够了. 学习笔记: 1.Android之如何获取短信验证码. 2.如 ...
- Android 学习笔记之如何使用SQLite数据库来保存数据...
PS:最近一阵子都在为考试复习...坑爹的计算机网络,复习了3天,最后该不会的还是不会...明天还考英语...真蛋疼... 学习内容: 1.使用SQLite数据库来保存数据... SQLite: ...
- Android学习笔记_43_网络通信之文件断点上传
1.建立服务端,用于接收上传的文件.这里使用Socket,文件可能会比较大.采用多线程编程,防止并发. package com.socket.service; import java.io.File; ...
- Android学习笔记(十二)——使用意图传递数据的几种方式
使用意图传递数据的几种方式 点此获取完整代码 我们除了要从活动返回数据,也经常要传递数据给活动.对此我们能够使用Intent对象将这些数据传递给目标活动. 1.创建一个名为PassingData的项目 ...
- Android学习笔记之网络接口(Http接口,Apache接口,Android接口)
目前Android平台有三种网络接口可以使用,他们分别是:Java.NET.*(标准Java接口),org.apache(Apache接口),和android.Net.*(android网络接口). ...
- AngularJS学习笔记(3)——通过Ajax获取JSON数据
通过Ajax获取JSON数据 以我之前写的与用户交互的动态清单列表为例,使用JSON前todo.html代码如下: <!DOCTYPE html> <html ng-app=&quo ...
- android学习笔记36——使用原始XML文件
XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...
随机推荐
- hadoop面试题一
1.hadoop运行的原理?xxxxxx 2.mapreduce的原理?xxxxxx 3.HDFS存储的机制?xxxxxx 4.举一个简单的例子说明mapreduce是怎么来运行的 ?xxxxxx 5 ...
- 给string添加新的函数
var str = "abcdefg";String.prototype.constr = function(){ return this.split('').join('-'); ...
- [转]Oracle job procedure 存储过程定时任务
本文转自:http://www.cnblogs.com/hoojo/p/oracle_procedure_job_interval.html oracle job有定时执行的功能,可以在指定的时间点或 ...
- Javaweb的get请求乱码解决
get方式请求:即将参数放在URL中,因此这就涉及到URL的编码了 方式一:[推荐] 方式二: 前端编码: encodeURI(encodeURI("")) 后端解码: java. ...
- IDEA+Maven+多个SpringBoot子模块(创建多模块整合项目)
https://blog.csdn.net/willjgl/article/details/77773634 https://blog.csdn.net/qqHJQS/article/details/ ...
- How can I use Server.MapPath() from global.asax?
引用: using System.Web.Hosting; string filePathcsv = ""; string file = ""; string ...
- Mybatis 的多个数据源
主要思路: DataSource --> SqlSessionFactory --> SqlSessionTemplate 第一方式: 定义多套 DataSource --> Sq ...
- PHP 运行环境和服务器相关配置
1.在DOS命令窗口输入 mysql -hlocalhost -uroot -p回车 进入mysql数据库, 其中-h表示服务器名,localhost表示本地:-u为数据库用户名,root是mysql ...
- SVNKit学习——使用High-Level API管理Working Copy示例(六)
本篇内容是基于SVNKit High-Level API实现的针对Working copy的操作,操作内容与SVN图形化界面.命令行类似. High-Level API类图: 核心思想: 所有操作由各 ...
- SQL Server ->> CLR编程问题汇总
1) CLR不支持C#类方法重载(Method Overload) 今天打算写个枚举目录的C# CLR存储过程,结果发现原来CLR不支持方法重载. Msg 6572, Level 16, State ...