加载图像的URL在Android中,只有小 [英] Load image from url in Android, only if small

查看:215
本文介绍了加载图像的URL在Android中,只有小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 BitmapFactory.de codeStream 来从Android的URL加载图像。我想只下载图像下方具有一定规模,并且我目前使用 getContentLength 进行检查。

不过,我听说 getContentLength 并不总是提供文件的大小,在这种情况下,我想尽快停止下载我知道文件过大。什么是正确的方式做到这一点?

下面是我目前的code。我现在返回null如果 getContentLength 并没有提供答案。

  HTTPGET HTT prequest =新HTTPGET(新的URL(urlString).toURI());
HttpClient的HttpClient的=新DefaultHttpClient();
HTT presponse响应=(HTT presponse)httpClient.execute(HTT prequest);
HttpEntity实体= response.getEntity();
BufferedHttpEntity bufHttpEntity =新BufferedHttpEntity(实体);
最终长CONTENTLENGTH = bufHttpEntity.getContentLength();
如果((CONTENTLENGTH> = 0&安培;及(最大长度== 0 || CONTENTLENGTH&所述;最大长度))){
    InputStream的是= bufHttpEntity.getContent();
    点阵位图= BitmapFactory.de codeStream(是);
    返回新BitmapDrawable(位);
} 其他 {
    返回null;
}
 

解决方案

您应该使用HttpHead发出一个HEAD请求,这是类似的,但还是会只返回头。如果内容长度是令人满意的,那么你可以让你的GET请求来获取内容。

大多数服务器应该处理HEAD请求没有问题,但偶尔的应用程序服务器将不期待它,将抛出一个错误。只是要注意的。

更新想我会尝试回答您的实际问题为好。你可能会有数据传递到BitmapFactory之前,将数据读入一个中间字节的缓冲区。

 的InputStream是= bufHttpEntity.getContent();
ByteArrayOutputStream字节= ByteArrayOutputStream();
byte []的缓冲区=新的字节[128];
INT读取;
INT totalRead = 0;
而((读取= is.​​read(缓冲液))大于0){
   totalRead + =读取;
   如果(totalRead> TOO_BIG){
     //中止下载。密切联系
     返回null;
   }
   bytes.write(缓冲,0读取);
}
 

无关,但记得要在使用后随时拨打consumeContent()的实体,这样HttpClient的可以重复使用的连接。

I'm using BitmapFactory.decodeStream to load an image from a url in Android. I want to only download images below a certain size, and I'm currently using getContentLength to check this.

However, I'm told that getContentLength doesn't always provide the size of the file, and in those cases I would like to stop the download as soon as I know that the file is too big. What is the right way to do this?

Here is my current code. I currently return null if getContentLength doesn't provide an answer.

HttpGet httpRequest = new HttpGet(new URL(urlString).toURI());
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpClient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); 
final long contentLength = bufHttpEntity.getContentLength();
if ((contentLength >= 0 && (maxLength == 0 || contentLength < maxLength))) {
    InputStream is = bufHttpEntity.getContent();
    Bitmap bitmap = BitmapFactory.decodeStream(is);
    return new BitmapDrawable(bitmap);
} else {
    return null;
}

解决方案

You should use HttpHead to issue a HEAD request, which is similar to GET but will return only headers. If the content length is satisfactory, then you can make your GET request to get the content.

The majority of servers should handle HEAD requests without a problem, but occasionally an application servers won't be expecting it and will throw an error. Just something to be aware of.

UPDATE thought I would try to answer your actual question as well. You will probably have to read the data into an intermediate byte buffer before passing the data to the BitmapFactory.

InputStream is = bufHttpEntity.getContent();
ByteArrayOutputStream bytes = ByteArrayOutputStream();
byte[] buffer = new byte[128];
int read;
int totalRead = 0;
while ((read = is.read(buffer)) > 0) {
   totalRead += read;
   if (totalRead > TOO_BIG) {
     // abort download. close connection
     return null;
   }
   bytes.write(buffer, 0 read);
}

Unrelated, but remember to always call consumeContent() on the entity after use so that HttpClient can reuse the connection.

这篇关于加载图像的URL在Android中,只有小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆