Android开发入门之网络通信(网络图片查看器)
16lz
2021-01-25
第一步:新建一个Android工程命名为netimage目录结构如下图:
第二步:修改activity_main.xml布局文件代码如下:
<LinearLayout 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:orientation="vertical" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/imagepath" /> <EditText android:id="@+id/et_imagepath" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/path" /> <Button android:id="@+id/btn_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/view" /> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"/></LinearLayout>
strings.xml:
<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">网络图片查看器</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="imagepath">网络图片路径</string> <string name="view">查看</string> <string name="success">获取图片成功!</string> <string name="fail">获取图片失败!</string> <string name="path">http://p1.qhimg.com/d/_onebox/search.png</string></resources>
第三步:编写MianActivity类:
package cn.leigo.netimage;import java.io.IOException;import cn.leigo.service.ImageService;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener {private EditText mImagePathEditText;private Button mViewButton;private ImageView mImageView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mImagePathEditText = (EditText) findViewById(R.id.et_imagepath);mViewButton = (Button) findViewById(R.id.btn_view);mImageView = (ImageView) findViewById(R.id.iv);mViewButton.setOnClickListener(this);}@Overridepublic void onClick(View v) {String imagePath = mImagePathEditText.getText().toString();byte[] data;try {data = ImageService.getImage(imagePath);Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);mImageView.setImageBitmap(bitmap); // 显示图片Toast.makeText(this, R.string.success, Toast.LENGTH_SHORT).show();} catch (IOException e) {e.printStackTrace();Toast.makeText(this, R.string.fail, Toast.LENGTH_SHORT).show();}}}
业务类ImageService:
package cn.leigo.service;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import cn.leigo.utils.StreamTool;public class ImageService {/** * 获取网络图片的数据 * * @param imagePath * 网络图片路径 * @return * @throws IOException */public static byte[] getImage(String imagePath) throws IOException {byte[] data = null;URL url = new URL(imagePath);HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 基于HTTP协议链接对象conn.setConnectTimeout(5000);conn.setRequestMethod("GET");if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {InputStream inputStream = conn.getInputStream();data = StreamTool.read(inputStream);}return data;}}
工具类StreamTool:
package cn.leigo.utils;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;public class StreamTool {/** * 读取流中的数据 * * @param inputStream * @return * @throws IOException */public static byte[] read(InputStream inputStream) throws IOException {ByteArrayOutputStream baos = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len = 0;while ((len = inputStream.read(buffer)) != -1) {baos.write(buffer, 0, len);}inputStream.close();return baos.toByteArray();}}
最后别忘了在AndroidManifest.xml文件中添加权限:
<uses-permission android:name="android.permission.INTERNET" />
运行上述工程查看效果图:
更多相关文章
- Nginx系列教程(六)| 手把手教你搭建 LNMP 架构并部署天空网络电影
- android图片拖动Gallery
- 【android开发】android文件搜索
- Android(安卓)TextView中显示图片
- AndroidStudio给kotlin添加类似logm的快捷方式(Live Templates)
- Android(安卓)- 视频提取图片方法
- Android端获取视频第一帧的方法
- android的MVC模式网络请求demo
- Android压缩图片到100K以下并保持不失真的高效方法 - feicien的