参考地址:
android 服务器获取图片路径,Android读取服务器图片的三种方法_猪猪堂客的博客-CSDN博客
Android HttpRequest_AaronZhao的博客-CSDN博客
Android get获取InputStream后转换不成bitmap,或者转换为bitmap不显示_android inputstream转bitmap_罴之力量的博客-CSDN博客
package com.pic.utils;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* 通过输出流的方式获取服务器中图片
* 说明:如果服务器中的图片在浏览器中直接打开出现下载时,在Android中
* 则不能通过WebView查看,需要通过图片地址获取到图片的流并转成bitmap才可以查看;
* 以下两种方法是通过HttpURLConnection或HttpClient的形式来获取输出流,通过测试
* HttpURLConnection不能正确获取到图片并会报405(猜测和后端配置有关);
* HttpClient通过GET请求则可以获取到图片内容,如果图片地址需要Cookies才可以
* 查看,则需要加请求头
*/
public class PICUtils {
//1.使用HttpURLConnection获取服务器图片流
public String getPIC(String fileName){
try {
// 根据地址创建URL对象
URL url = new URL(MyUrl.url +fileName);
// 根据URL对象打开链接
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
// 设置请求的方式
urlConnection.setRequestMethod("GET");
// 设置请求的超时时间
urlConnection.setReadTimeout(1000*30);
urlConnection.setConnectTimeout(1000*30);
// 传递的数据
String data = "";
urlConnection.setRequestProperty("Cookie","");
// 设置请求的头
urlConnection.setRequestProperty("Connection", "keep-alive");
// 设置请求的头
urlConnection.setRequestProperty("Content-Type",
"application/json");
// 设置请求的头
urlConnection.setRequestProperty("Content-Length",
String.valueOf(data.getBytes().length));
// 设置请求的头
urlConnection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.3; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0");
urlConnection.setDoOutput(true); // 发送POST请求必须设置允许输出
urlConnection.setDoInput(true); // 发送POST请求必须设置允许输入
//setDoInput的默认值就是true
//获取输出流
OutputStream os = urlConnection.getOutputStream();
os.write(data.getBytes());
os.flush();
if (urlConnection.getResponseCode() == 200) {
// 获取响应的输入流对象
InputStream is = urlConnection.getInputStream();
//1.获取Bitmap
//(1)
// Bitmap bitmap = BitmapFactory.decodeStream(is);
//(2)如果上边不能正确获取到bitmap则使用以下方法
// byte[] bytes = readStream(is);
//Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
//2.创建字节输出流对象
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 定义读取的长度
int len = 0;
// 定义缓冲区
byte buffer[] = new byte[1024];
// 按照缓冲区的大小,循环读取
while ((len = is.read(buffer)) != -1) {
// 根据读取的长度写入到os对象中
baos.write(buffer, 0, len);
}
// 释放资源
is.close();
baos.close();
// 返回字符串
String result = new String(baos.toByteArray());
System.out.println("getPIC1:::"+result);
return result;
} else {
System.out.println("getPIC1:::"+urlConnection.getResponseCode());
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("getPIC1:::"+e.fillInStackTrace());
}
return null;
}
//2.使用HttpClient获取服务器图片流
public String getPIC2(String fileName){
try {
//生成一个请求对象
HttpGet httpGet = new HttpGet(MyUrl.url +fileName);
//请求头
httpGet.addHeader("Cookie","自己的Cookie");
//生成一个Http客户端对象
HttpClient httpClient = new DefaultHttpClient();
//httpResponse就是代表响应对象
HttpResponse httpResponse=httpClient.execute(httpGet);
int i = httpResponse.getStatusLine().getStatusCode();
if (i==200){
//httpEntity包含的就是返回的消息内容
HttpEntity httpEntity = httpResponse.getEntity();
//1.获取响应数据String
// InputStream inputStream =httpEntity.getContent();
// BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
// StringBuilder result=new StringBuilder("");
// String line = "";
// while((line=reader.readLine())!=null){
// result.append(line);
// }
// inputStream.close();
// System.out.println("getPIC2:::"+result.toString());
// return result.toString();
//2.获取Bitmap
BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(httpEntity);
InputStream is = bufferedHttpEntity.getContent();
byte[] bytes = readStream(is);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return "";
}else{
System.out.println("getPIC2:::"+i);
}
} catch (Exception e) {
System.out.println("getPIC2:::"+e.fillInStackTrace());
}
return null;
}
//输入流转bitmap
public static byte[] readStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] bytes = outStream.toByteArray();
outStream.close();
inStream.close();
return bytes;
}
}