Android实现网络图片查看器和网页源码查看器(2)
来源:未知 责任编辑:责任编辑 发表时间:2015-09-09 15:30 点击:次
}
}catch (Exception e) {
Toast.makeText(getApplicationContext(), R.string.error, 1).show();
}
}
});
}
}</span>
view plain
<span style="FONT-WEIGHT: normal">public class ImageService {
/**
* 获取图片
* @param path 网络图片路径
* @return 图片的字节数据
*/
public static byte[] getImage(String path) throws Exception{
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置超时时间
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode()==200){
InputStream inStream = conn.getInputStream();
byte[] data = StreamTool.read(inStream);
return data;
}
return null;
}
}</span>
view plain
<span style="FONT-WEIGHT: normal">public class StreamTool {
/**
* 读取输入流数据
* @param inStream
* @return
*/
public static byte[] read(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>