Android的下载位图回报的空有时 [英] Android Download of bitmap returns null sometimes

查看:103
本文介绍了Android的下载位图回报的空有时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面ASYN任务的code下载一个位图被添加到我的自定义类。但有时它返回空值,没有I​​OException异常或任何异常。我不是很肯定可以做什么

 公共位图downloadFile(字符串fileUrl){
    URL myFileUrl = NULL;
    尝试 {
        myFileUrl =新的URL(fileUrl);
    }赶上(MalformedURLException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }

    尝试 {
        HttpURLConnection的康恩=(HttpURLConnection类)myFileUrl.openConnection();
        conn.setDoInput(真正的);
        //conn.setReadTimeout(500000000);
        conn.connect();
        InputStream的是= conn.getInputStream();
           BitmapFactory.Options O =新BitmapFactory.Options();
            o.inJustDe codeBounds = TRUE;
            BitmapFactory.de codeStream(是,空,O);
        is.close();
        conn.disconnect();
        int标= 1;
        INT IMAGE_MAX_SIZE = 400;
        如果(o.outHeight> IMAGE_MAX_SIZE || o.outWidth> IMAGE_MAX_SIZE){
            标度=(int)的Math.pow(2,(int)的Math.round(将Math.log(IMAGE_MAX_SIZE /(双)Math.max(o.outHeight,o.outWidth))/将Math.log(0.5)));
        }
        //德code与inSampleSize
        BitmapFactory.Options O2 =新BitmapFactory.Options();
        o2.inSampleSize =规模;
        康恩=(HttpURLConnection类)myFileUrl.openConnection();
        conn.setDoInput(真正的);
        //conn.setReadTimeout(500000000);
        conn.connect();
        是= conn.getInputStream();
       位图B = BitmapFactory.de codeStream(是,空,O2);
       如果(二== NULL)
           Log.e(Config.log_id,下载图像失败);
    返回b;

    }

    赶上(IOException异常E){
        Log.e(Config.log_id,下载图像失败+ e.getMessage());
        e.printStackTrace();
    }
    返回null;
}
 

解决方案

我遇到了这个问题,以及使用非缓冲变量时, 从URL中获取的时候流的读者。

这工作对我来说最简单的解决方案是使用一个BufferedHttpEntity获取图像数据。

 进口org.apache.http.Htt presponse;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.methods.HttpGet;
进口org.apache.http.entity.BufferedHttpEntity;

公共静态位图德codeFromUrl(HttpClient的客户端,网址URL,配置bitmapCOnfig)
    {
        HTT presponse响应= NULL;
        位图B = NULL;
        InputStream的河道= NULL;

        BitmapFactory.Options德codeOptions =新BitmapFactory.Options();
        德codeOptions.in preferredConfig = bitmapCOnfig;
        尝试
        {
            HTTPGET请求=新HTTPGET(url.toURI());
            响应= client.execute(要求);
            如果(response.getStatusLine()的getStatus code()!= 200)
            {
                MyLogger.w(+ url.toString的不良反应());
                MyLogger.w(HTTP响应:+ response.getStatusLine()的toString());
                返回null;
            }
            BufferedHttpEntity bufHttpEntity =新BufferedHttpEntity(response.getEntity());
            河道= bufHttpEntity.getContent();

            返回BitmapFactory.de codeStream(河道内,空,德codeOptions);
        }
        赶上(例外前)
        {
            MyLogger.e(从错误解码位图:+网址,前);
            如果(响应!= NULL)
            {
                MyLogger.e(HTTP状态:+ response.getStatusLine()的getStatus code());
            }
            返回null;
        }
        最后
        {
            如果(河道!= NULL)
            {
                尝试 {
                    instream.close();
                }赶上(IOException异常E){
                    MyLogger.e(错误收盘流,E);
                }
            }
        }
    }
 

I am using the code below in asyn task to download a bitmap to be added to my custom class. however sometimes it return nulls with no IOException or any exception. i am not very sure what can be done

    public  Bitmap downloadFile(String fileUrl){
    URL myFileUrl =null;          
    try {
        myFileUrl= new URL(fileUrl);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
        conn.setDoInput(true);
        //conn.setReadTimeout(500000000);
        conn.connect();
        InputStream is = conn.getInputStream();
           BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(is, null, o);
        is.close();
        conn.disconnect();
        int scale = 1;
        int IMAGE_MAX_SIZE=400;
        if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
            scale = (int)Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
        }
        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        conn= (HttpURLConnection)myFileUrl.openConnection();
        conn.setDoInput(true);
        //conn.setReadTimeout(500000000);
        conn.connect();
        is  =  conn.getInputStream();
       Bitmap b = BitmapFactory.decodeStream(is, null, o2);
       if (b==null)
           Log.e(Config.log_id, " Download image failed");
    return b;

    }

    catch (IOException e) {
        Log.e(Config.log_id, " Download image failed"+e.getMessage());
        e.printStackTrace();
    }
    return null;
}

解决方案

I have run into this as well when using unbuffered variants of stream readers when getting from urls.

The simple solution that worked for me was to use a BufferedHttpEntity to get the image data.

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;

public static Bitmap decodeFromUrl(HttpClient client, URL url, Config bitmapCOnfig)
    {
        HttpResponse response=null;
        Bitmap b=null;
        InputStream instream=null;

        BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
        decodeOptions.inPreferredConfig = bitmapCOnfig;
        try
        {
            HttpGet request = new HttpGet(url.toURI());
            response = client.execute(request);
            if (response.getStatusLine().getStatusCode() != 200)
            {
                MyLogger.w("Bad response on " + url.toString());
                MyLogger.w ("http response: " + response.getStatusLine().toString());
                return null;
            }
            BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(response.getEntity());
            instream = bufHttpEntity.getContent();

            return BitmapFactory.decodeStream(instream, null, decodeOptions);
        }
        catch (Exception ex)
        {
            MyLogger.e("error decoding bitmap from:" + url, ex);
            if (response != null)
            {
                MyLogger.e("http status: " + response.getStatusLine().getStatusCode());
            }
            return null;
        }
        finally
        {
            if (instream != null)
            {
                try {
                    instream.close();
                } catch (IOException e) {
                    MyLogger.e("error closing stream", e);
                }
            }
        }
    }

这篇关于Android的下载位图回报的空有时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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