安卓:当下载图像误差大 [英] Android : Big error while downloading images

查看:157
本文介绍了安卓:当下载图像误差大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经试过下载图像的三种方式。所有的#1显示的成员。
所有的三种方法无法下载所有从服务器中的图像。很少被下载,很少不是。

I have tried three ways of downloading images. All suggest by members of Stackoverflow . All the three methods fail to download all the images from the server. Few are downloaded and few are not.

我注意到一件事,每个方法都无法从特定位置下载图像。
这是第3种方法总是不能下载的前三个图像。我改变了形象,但即使这样,前三个图像不会下载。

I noticed a thing that each of the method fail to download image from particular position. That is method 3 always fails to download the first three images. I changed the images but even then , the first three images are not downloaded.

方法1:

 public Bitmap downloadFromUrl( String imageurl )
   {
       Bitmap bm=null;
       String imageUrl = imageurl;
       try {
               URL url = new URL(imageUrl); //you can write here any link

              URLConnection ucon = url.openConnection();


             InputStream is = ucon.getInputStream();
              BufferedInputStream bis = new BufferedInputStream(is);


               ByteArrayBuffer baf = new ByteArrayBuffer(50);
              int current = 0;
              while ((current = bis.read()) != -1) {
                       baf.append((byte) current);
              }

             bm= BitmapFactory.decodeByteArray(baf.toByteArray(), 0, baf.toByteArray().length);


     } catch (IOException e) {
               Log.d("ImageManager", "Error: " + e);
     }
       return bm;

   }

在这里,我得到错过图像错误是:SKIimagede codeR,工厂返回null

Here the error i get for missed images is :SKIimagedecoder , the factory returned null.

方法:2

 public static Bitmap loadBitmap(String url) 
   {
       Bitmap bitmap = null;
       InputStream in = null;
       BufferedOutputStream out = null;

       try {
           in = new BufferedInputStream(new URL(url).openStream(), 4*1024);

           final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
           out = new BufferedOutputStream(dataStream,  4 * 1024);

           int byte_;
           while ((byte_ = in.read()) != -1)
               out.write(byte_);
           out.flush();

           final byte[] data = dataStream.toByteArray();
           BitmapFactory.Options options = new BitmapFactory.Options();
           //options.inSampleSize = 1;

           bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
       } catch (IOException e) {
           System.out.println(e);
           Log.e("","Could not load Bitmap from: " + url);
       } finally {
           try{
               in.close();
               out.close();
           }catch( IOException e )
           {
               System.out.println(e);
           }
       }

       return bitmap;
   }

我来到这里的错误是同上。

The error i get here is same as above.

方法3:

private Bitmap downloadFile(String fileUrl){
    URL bitmapUrl =null;  
    Bitmap bmImg = null;
    try {
        bitmapUrl= new URL(fileUrl);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    HttpGet httpRequest = null;

    try {
            httpRequest = new HttpGet(bitmapUrl.toURI());
    } catch (URISyntaxException e) {
            e.printStackTrace();
    }

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = (HttpResponse) httpclient
                .execute(httpRequest);

        HttpEntity entity = response.getEntity();
        BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
        InputStream instream = bufHttpEntity.getContent();
        bmImg = BitmapFactory.decodeStream(instream);
    } catch (Exception e) {
            System.out.println(e);
    }
    return bmImg;
}

我来到这里的错误是:org.apache.http.NoHtt presponseException:目标服务器无法响应。

The error i get here is : org.apache.http.NoHttpResponseException: The target server failed to respond.

请帮忙。它是唯一从完成项目阻止我。

please help. It is the only thing stopping me from completing the project.

推荐答案

这可能是因为你的位图的创建需要时间,因此连接超时。你可能得到的所有inputstreams引用,然后下载并创建。这是一个粗略的切的答案,但如果推理是正确的,你可以改善它:

It's possible that your creation of bitmaps takes time and therefore the connection times out. You could possibly get references to all the inputstreams and then download and create. This is a rough-cut answer, but if the reasoning is right, you can improve on it:

public Bitmap[] downloadFromUrl(String[] imageUrls)
{
    Bitmap[] bm = new Bitmap[imageUrls.length];
    BufferedInputStream[] bis = new BufferedInputStream[imageUrls.length];
    for (int i = 0; i < imageUrls.length; i++)
    {
        try
        {
            URL url = new URL(imageUrls[i]); // you can write here any link
            URLConnection ucon = url.openConnection();

            InputStream is = ucon.getInputStream();
            bis[i] = new BufferedInputStream(is);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    for (int i = 0; i < bis.length; i++)
    {
        try
        {
            ByteArrayBuffer baf = new ByteArrayBuffer(50);
            int current = 0;
            while ((current = bis[i].read()) != -1)
            {
                baf.append((byte) current);
            }

            bm[i] = BitmapFactory.decodeByteArray(baf.toByteArray(), 0, baf.toByteArray().length);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    return bm;

}

这篇关于安卓:当下载图像误差大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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