android中通过Http访问图片工具类的实现
2013-12-23 21:25
531 查看
网络上获取图片信息的工具类的实现:
package com.demo.loading; import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.http.AndroidHttpClient; public class ImageGetForHttp { /** * 通过URL获取bitmap对象 * * @param url * @param type * @return */ public static Bitmap downloadBitmap(String url, String type) { final HttpClient client = AndroidHttpClient.newInstance("Android"); final HttpGet getRequest = new HttpGet(url); try { HttpResponse response = client.execute(getRequest); final int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { return null; } final HttpEntity entity = response.getEntity(); if (entity != null) { InputStream inputStream = null; try { inputStream = entity.getContent(); FilterInputStream fit = new FlushedInputStream(inputStream); Bitmap bm = BitmapFactory.decodeStream(fit); Bitmap bitmap = ImageRedraw.getInstance().redrawImg(bm, type); return bitmap; } finally { if (inputStream != null) { inputStream.close(); } entity.consumeContent(); } } } catch (IOException e) { getRequest.abort(); e.printStackTrace(); } catch (IllegalStateException e) { getRequest.abort(); e.printStackTrace(); } catch (Exception e) { getRequest.abort(); e.printStackTrace(); } finally { if ((client instanceof AndroidHttpClient)) { ((AndroidHttpClient) client).close(); } } return null; } /* * An InputStream that skips the exact number of bytes provided, unless it * reaches EOF. */ static class FlushedInputStream extends FilterInputStream { public FlushedInputStream(InputStream inputStream) { super(inputStream); } @Override public long skip(long n) throws IOException { long totalBytesSkipped = 0L; while (totalBytesSkipped < n) { long bytesSkipped = in.skip(n - totalBytesSkipped); if (bytesSkipped == 0L) { int b = read(); if (b < 0) { break; // we reached EOF } else { bytesSkipped = 1; // we read one byte } } totalBytesSkipped += bytesSkipped; } return totalBytesSkipped; } } /** * 通过URL获取字节数组对象 * * @param hostURL * @param headParams * @return */ public static byte[] getStream(final String hostURL) { HTTPRequest httpGet = new HTTPRequest(); byte[] BufferData = null; try { BufferData = httpGet.httpRequestByteArray(hostURL); } catch (Exception e) { e.printStackTrace(); } return BufferData; } }
相关文章推荐
- 在Android上通过模拟HTTP multipart/form-data请求协议信息实现图片上传
- 在 Android 上通过模拟 HTTP multipart/form-data 请求协议信息实现图片上传
- 在 Android 上通过模拟 HTTP multipart/form-data 请求协议信息实现图片上传
- 在 Android 上通过模拟 HTTP multipart/form-data 请求协议信息实现图片上传
- [Android] 通过Menu实现图片怀旧、浮雕、模糊、光照和素描效果
- android上传图片到服务器(使用base64字节流的形式通过 AsyncHttpClient框架传输)
- Android 通过软引用实现图片缓存,防止内存溢出
- Android通过WebService实现图片的上传和下载(一)
- android通过http上传文件(图片)
- 【android】实现图片的三级缓存。工具类
- 10_Android中通过HttpUrlConnection访问网络,Handler和多线程使用,读取网络html代码并显示在界面上,ScrollView组件的使用
- 基于HttpClient4.5.1实现Http访问工具类
- Android 通过WebService进行网络编程,使用工具类轻松实现
- 系出名门Android(10) - HTTP 通信, XML 解析, 通过 Hander 实现异步消息处理
- Android文件图片上传的详细讲解(一)HTTP multipart/form-data 上传报文格式实现手机端上传
- Android模拟 HTTP multipart/form-data 请求协议信息实现图片上传
- Android通过HttpURLConnection和HttpClient接口实现网络编程
- 【通过HTTP/2实现每天处理400GB图片的实践
- 一个android访问http资源的便捷工具类——HttpHelper
- [置顶] Android开发之下载服务器上的一张图片到本地java代码实现HttpURLConnection