android开发之获取网络数据
来源:互联网 发布:java调用chrome内核 编辑:程序博客网 时间:2024/06/05 00:27
开发之获取网络数据
获取网络图片
首先我们需要把界面搭建好
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/showImageID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn" />
//用来点击事件
<ImageView
android:id="@+id/imageViewID"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
//用来显示图片
</LinearLayout>
------------------------------------------------------------------------------------
package cn.class3g.service;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class ImageService {
public static byte[] getImageData(String path) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
InputStream inStream = conn.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
byte[] data = bos.toByteArray();
return data;
}
}
--------------------------------------------------------------------------------------
最后就是我们的activity
package cn.class3g.net;
import cn.class3g.service.ImageService;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class ShowImageActivity extends Activity implements OnClickListener {
Button btn;
ImageView imageView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
}
private void findViews() {
btn = (Button) this.findViewById(R.id.showImageID);
imageView = (ImageView) this.findViewById(R.id.imageViewID);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String path = "http://cimg2.163.com/catchpic/4/44/44ED4853B4A3FE71971C885A0A22825E.jpg";
try {
byte data[] = ImageService.getImageData(path);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
imageView.setImageBitmap(bitmap);
} catch (Exception e) {
Log.e("tag", e.toString());
Toast.makeText(this, R.string.error, Toast.LENGTH_SHORT).show();
}
}
}
- android开发之获取网络数据
- Android应用开发之获取网络数据
- Android应用开发之获取网络数据
- android开发之获取网络数据
- Android开发之从网络(Intent)获取和发送数据
- Android网络开发之实时获取最新数据
- Android 网络获取数据之XML解析
- Android 网络获取数据之Json解析
- Android开发之网络图片获取
- Android获取网络数据
- android获取网络数据
- android获取网络数据
- Kotlin开发Android笔记7:Anko获取网络数据
- Android成长之路-获取网络数据(图片)
- android之访问网络获取网页数据并存入数据库
- Android开发技术之──获取网络图片
- Android中获取网络数据
- android—获取网络数据
- QT拖拽功能
- 用C++程序理解汉字的机内码表示
- C语言 关机程序
- Java容器中HashMap的基本用法
- 2014年实习生招聘之百度上海研发中心实习生招聘电话面试—2014/04/18
- android开发之获取网络数据
- OpenCV 鼠标响应函数 ,用鼠标选中一块图像区域,然后计算选中的ROI区域的统计直方图
- 模式识别ID3算法实现
- C++模版初探之函数模板(一)
- 学习日志
- 对linux档案系统以及磁盘管理的一点总结(三)
- The world’s two worst variable names
- 把自己的电脑做服务器发布tomcat的项目外网访问
- jdbc总结