现在的手机以离不开网络了,而且网络速度也越来越快,所以网络HTTP协议请求是必不可少的,现在我们 简单讲解一下网路图片的获取,这里只是讲解一下获取原理,并不是造轮子给大家用,最后会推荐几个轮子给大家用。
逻辑:
- 首先需要一个图片地址的URL
- 然后需要通过HTTP网络请求协议在线获取图片
- 再将图片转换成一个 Bitmap(这里用Bitmap)
- 在将图片展示到控件上
Activity代码:
package com.example.administrator.foundationdemo.http;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.example.administrator.foundationdemo.R;
import com.example.administrator.foundationdemo.http.service.ImageService;
import java.io.IOException;
public class HttpImageActivity extends AppCompatActivity {
private EditText http_image_edittext;
private Button http_image_button;
private ImageView http_image_imageview;
private String urlString = "https://img.haomeiwen.com/i4047773/f6c7549ab7c95bd2?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_http_image);
init();
}
private void init(){
http_image_edittext = (EditText) findViewById(R.id.http_image_edittext);
http_image_imageview = (ImageView) findViewById(R.id.http_image_imageview);
http_image_button = (Button) findViewById(R.id.http_image_button);
http_image_edittext.setText(urlString);
http_image_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setImage();
}
});
}
//开启子线程,进行耗时操作
private void setImage(){
Log.d("FLY","请求失败1");
new Thread(new Runnable() {
@Override
public void run() {
String path = http_image_edittext.getText().toString();
byte[] date = new byte[0];
try {
date = ImageService.getImage(urlString);
} catch (IOException e) {
e.printStackTrace();
Log.d("FLY", "获取图片异常\n" + e.toString());
Toast.makeText(HttpImageActivity.this,"获取图片失败",Toast.LENGTH_SHORT).show();
}
Log.d("FLY","请求失败1");
Bitmap bitmap = BitmapFactory.decodeByteArray(date,0,date.length);
Message msg = new Message();
msg.obj = bitmap;
hadler.sendMessage(msg);
}
}).start();
}
Handler hadler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Log.d("FLY", "请求失败2");
Bitmap bitmap = (Bitmap) msg.obj;
http_image_imageview.setImageBitmap(bitmap);
}
};
}
ImageService代码:
package com.example.administrator.foundationdemo.http.service;
import android.util.Log;
import android.widget.Toast;
import com.example.administrator.foundationdemo.http.tool.StreamTool;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by Administrator on 2016/12/29.
*/
public class ImageService {
public static byte[] getImage(String path) throws IOException {
URL url = new URL(path);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();//得到基于HTTP协议的链接对象
httpURLConnection.setConnectTimeout(5000);//请求超时时间
httpURLConnection.setRequestMethod("GET");//请求方式
if (httpURLConnection.getResponseCode() == 200){
InputStream inputStream = httpURLConnection.getInputStream();
return StreamTool.readInputStream(inputStream);
}else {
Log.d("FLY","请求失败"+httpURLConnection.getResponseCode());
}
return null;
}
}
StreamTool 代码:
package com.example.administrator.foundationdemo.http.tool;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by Administrator on 2017/1/3.
*/
public class StreamTool {
public static byte[] readInputStream(InputStream inputStream) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int bytrsLen = 0;
while((bytrsLen =inputStream.read(bytes)) != -1){
outputStream.write(bytes,0,bytrsLen);
}
inputStream.close();
return outputStream.toByteArray();
}
}
XML代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/MatchMatch"
android:orientation="vertical"
tools:context="com.example.administrator.foundationdemo.http.HttpImageActivity">
<TextView
style="@style/WrapWrap"
android:text="网络图片地址"/>
<EditText
android:id="@+id/http_image_edittext"
style="@style/MatchWrap"/>
<Button
android:id="@+id/http_image_button"
style="@style/WrapWrap"
android:text="获取" />
<ImageView
android:id="@+id/http_image_imageview"
android:layout_width="100dp"
android:layout_height="100dp" />
</LinearLayout>
说好的向大家推荐几个很好用的轮子:
网友评论