网络访问之图片显示
2015-07-25 19:09
633 查看
注意:访问网络必须加上访问网络的权限:
<uses-permission android:name="android.permission.INTERNET"/>
1.布局代码略:
avtivity部分代码如下:
public class MainActivity extends Activity {
private EditText path;
private Button button;
private ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
path=(EditText) findViewById(R.id.edit_1);
image=(ImageView) findViewById(R.id.image_1);
button=(Button) findViewById(R.id.button_1);
button.setOnClickListener(new buttonOnClick());
}
public final class buttonOnClick implements OnClickListener{
@Override
public void onClick(View v) {
//获得图片路径
String pathText= path.getText().toString();
//将获取的图片以字节的形式存放
byte[] data;
try {
//创建逻辑类ImageService以及方法getImage方法
data = ImageService.getImage(pathText);
//将获取的图片数据转换成Bitmap对象
Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);
//设置显示图片
image.setImageBitmap(bitmap);
} catch (Exception e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "获取图片失败",Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
}
ImageService部分代码如下:
package com.qq1009108034.service;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.qq1009108034.bean.StreamTool;
/*
* 获取网络图片的数据
*/
public class ImageService {
public static byte[] getImage(String pathText) throws Exception {
URL url=new URL(pathText);
//打开请求
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
//设置请求是否超时
conn.setReadTimeout(5000);
//设置请求的方法
conn.setRequestMethod("GET");
//判断请求是否成功
if(conn.getResponseCode()==200){
InputStream inStream=conn.getInputStream();
return StreamTool.read(inStream);
}
return null ;
}
}
工具类:
public class StreamTool {
public static byte[] read(InputStream inStream) throws IOException {
ByteArrayOutputStream outStream=new ByteArrayOutputStream();
byte[] buffer =new byte[1024];
int len=0;
while((len=inStream.read(buffer))!=-1){
outStream.write(buffer);
}
inStream.close();
//返回出内存中的数据
return outStream.toByteArray();
}
}
<uses-permission android:name="android.permission.INTERNET"/>
1.布局代码略:
avtivity部分代码如下:
public class MainActivity extends Activity {
private EditText path;
private Button button;
private ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
path=(EditText) findViewById(R.id.edit_1);
image=(ImageView) findViewById(R.id.image_1);
button=(Button) findViewById(R.id.button_1);
button.setOnClickListener(new buttonOnClick());
}
public final class buttonOnClick implements OnClickListener{
@Override
public void onClick(View v) {
//获得图片路径
String pathText= path.getText().toString();
//将获取的图片以字节的形式存放
byte[] data;
try {
//创建逻辑类ImageService以及方法getImage方法
data = ImageService.getImage(pathText);
//将获取的图片数据转换成Bitmap对象
Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);
//设置显示图片
image.setImageBitmap(bitmap);
} catch (Exception e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "获取图片失败",Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
}
ImageService部分代码如下:
package com.qq1009108034.service;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.qq1009108034.bean.StreamTool;
/*
* 获取网络图片的数据
*/
public class ImageService {
public static byte[] getImage(String pathText) throws Exception {
URL url=new URL(pathText);
//打开请求
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
//设置请求是否超时
conn.setReadTimeout(5000);
//设置请求的方法
conn.setRequestMethod("GET");
//判断请求是否成功
if(conn.getResponseCode()==200){
InputStream inStream=conn.getInputStream();
return StreamTool.read(inStream);
}
return null ;
}
}
工具类:
public class StreamTool {
public static byte[] read(InputStream inStream) throws IOException {
ByteArrayOutputStream outStream=new ByteArrayOutputStream();
byte[] buffer =new byte[1024];
int len=0;
while((len=inStream.read(buffer))!=-1){
outStream.write(buffer);
}
inStream.close();
//返回出内存中的数据
return outStream.toByteArray();
}
}
相关文章推荐
- nfs基本应用之httpd+nfs负载均衡实验
- HTTP Web服务器研究之二 HTTP协议
- WARN [http-8080-1] - No mapping found for HTTP request with URI [/MyLibrary/books/listBooks.action]
- 黑马程序员——Java网络编程
- 径向基神经网络(实例故障分类)
- [知识点]网络流之Dinic算法
- Windows网络编程:阻塞模式下的Socket编程
- TCP数据段格式+UDP数据段格式详解
- Android的http两种请求方式
- 黑马程序员--网络编程
- [知识点]网络流之Edmond-Karp算法
- HttpUrlConnection上传图片代码实现
- tcpdump丢包问题解决方法
- http form get post本质区别
- 以太网帧格式、IP数据报格式、TCP段格式+UDP段格式 详解
- poj 1679 http://poj.org/problem?id=1679
- [网络流24题] 03 最小路径覆盖问题(有向无环图最小路径覆盖,网络最大流)
- ubuntu设置系统时间与网络时间同步(转)
- ubuntu设置系统时间与网络时间同步(转)
- tcp/ip学习笔记