android httpUrlConnection 方式从网络上面下载图片
来源:互联网 发布:javascript 文字滚动 编辑:程序博客网 时间:2024/06/05 18:23
URL url = null;
HttpURLConnection conn = null;
InputStream in = null;
ByteArrayOutputStream outStream = null;
try
{
url = new URL("http://www.eoeandroid.com/data/attachment/forum/201107/18/142935bbi8d3zpf3d0dd7z.jpg");
conn = ( HttpURLConnection ) url.openConnection();
byte[] buffer = new byte[1024];
int len = -1;
while( (len = in.read(buffer)) != -1 ){
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
Bitmap btm = BitmapFactory.decodeByteArray(data, 0, data.length);
}catch( Exception ex )
{
ex.printStackTrace();
}finally
{
if( null != conn )
{
conn.disconnect();
}
if( null != in )
{
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if( null != outStream )
{
try {
outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
}
}
HttpURLConnection conn = null;
InputStream in = null;
ByteArrayOutputStream outStream = null;
try
{
url = new URL("http://www.eoeandroid.com/data/attachment/forum/201107/18/142935bbi8d3zpf3d0dd7z.jpg");
conn = ( HttpURLConnection ) url.openConnection();
in = conn.getInputStream();
//也可以直接 BitmapFactory.decodeStream(in); 出bitmap 要注意的是这样调用后,会把in里面的数据消耗了,就不能继续像下面那样读取了。
//in里面的数据只能用一次
outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while( (len = in.read(buffer)) != -1 ){
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
Bitmap btm = BitmapFactory.decodeByteArray(data, 0, data.length);
}catch( Exception ex )
{
ex.printStackTrace();
}finally
{
if( null != conn )
{
conn.disconnect();
}
if( null != in )
{
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if( null != outStream )
{
try {
outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
}
}
- android httpUrlConnection 方式从网络上面下载图片
- Android HttpURLConnection获取网络下载图片POST请求方式
- 使用HttpURLConnection从网络中下载图片
- HttpURLConnection下载网络图片
- HttpURLConnection下载网络图片
- Android - HttpURLConnection下载网络文件,加载网络图片
- HttpURLConnection从网上下载图片
- 第四章 网络下载之httpurlconnection 从网络上下载一张图片,在本地显示
- Android使用HttpURLConnection下载图片
- android httpurlconnection 方式上传图片
- Android HttpURLConnection 加载网络图片
- android 从网络上下载图片
- Android从网络上下载图片实现
- Android 从网络下载图片OOM
- Android网络稳定下载图片方式之一
- HttpURLConnection方式网上下载多张图片
- AsyncTask与HttpURLConnection下载网络图片
- 使用HttpURLconnection从网络获取图片
- 浏览器兼容性差异
- eclipse导出jar包
- Yii Url重写
- Linux rename命令 批量重命名
- eclipse Unable Run on hadoop? 解决办法
- android httpUrlConnection 方式从网络上面下载图片
- Vim Commands Cheat Sheet
- [转载]基于ssl加密通信的rmi应用实现
- maven编译报错Cannot locate the chosen ObjectFactory implementation: spring - [unknown locatio
- SqlServer中检查对象是否已经存在
- linux下打印函数调用栈(stack backtrace in Linux)
- android httpurlconnection 方式上传图片
- php网站调试
- php项目开发问题汇总