23_传智播客Android视频教程_网络通信之网络图片查看器
来源:互联网 发布:windows历代系统 编辑:程序博客网 时间:2024/05/01 17:46
布局文件:
<?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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="网络图片的地址是:\n 这个可以自定在输入框里面输入,但是为了简便,直接使用TextView做了" /> <Button android:id="@+id/Button01" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="得到网络图片" /> <ImageView android:id="@+id/imageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
MainActivity:
public class MainActivity extends Activity {private Button Button01;private ImageView imageView01;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button01 = (Button) findViewById(R.id.Button01);imageView01 = (ImageView) findViewById(R.id.imageView01);Button01.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String path = "http://111.114.116.236:8080/Test/image/Test.jpg";try {byte[] data = ImageService.getImageByByte(path);Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,data.length);imageView01.setImageBitmap(bitmap);} catch (Exception e) {e.printStackTrace();}}});}}
ImageService:
public class ImageService {public static byte[] getImageByByte(String path) throws Exception {ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();URL url = new URL(path);// HttpClientConnection 是对HttpURLConnection的封装,不建议使用/*HttpsURLConnection与HttpURLConnection就是http与https的区别 * */HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();httpURLConnection.setConnectTimeout(5000);httpURLConnection.setRequestMethod("GET");if (httpURLConnection.getResponseCode() == 200) {InputStream inputStream = httpURLConnection.getInputStream();byte[] buffer = new byte[1024];int len = 0;// 读到buffer中间while ((len = inputStream.read(buffer)) != -1) {// 从buffer中读数据byteArrayOutputStream.write(buffer, 0, len);}inputStream.close();}return byteArrayOutputStream.toByteArray();}}
- 23_传智播客Android视频教程_网络通信之网络图片查看器
- Android学习笔记---23_网络通信之网络图片查看器
- Android学习笔记---24_网络通信之网页源码查看器
- Android学习--网络通信之网络图片查看器
- Android开发入门之网络通信(网络图片查看器)
- Android网络通信之网络图片查看器
- 案例1 _网络图片查看器
- Android开发_网络编程之Http通信
- 实习日志(11):Android网络通信之网络图片查看器
- Android入门之网络图片查看器
- android学习之网络图片查看器
- Android入门之网络图片查看器
- Android入门之网络图片查看器
- android网络通信之获取网络图片
- android网络通信之获取网络图片
- Android 网络图片查看器
- Android网络图片查看器
- Android网络图片查看器
- SQL 中的事务
- matlab 线箱图制作
- 分辨率像素android手机屏幕分辨率 及 sp dip(dp) px 区别 及高中低分辨率时处理
- 那些帮助你成为优秀前端工程师的讲座——《性能篇》
- 屏蔽敏感词汇&KMP
- 23_传智播客Android视频教程_网络通信之网络图片查看器
- 对象的序列化和反序列化及其实现和使用
- C/C++常用IO函数
- java程序优化一些实践
- hdu2222 Keywords Search
- 【<私活案例1>】 某游戏网首页PS设计和Html静态效果源码
- poj--1470--Closest Common Ancestors--LCA
- 入门视频采集与处理(学会分析YUV数据)
- 微信 一亿用户背后架构秘密