【问题标题】:Android : Big error while downloading imagesAndroid:下载图像时出现大错误
【发布时间】:2011-03-19 06:43:56
【问题描述】:

我尝试了三种下载图像的方法。所有这些都由 Stackoverflow 的成员提出。 这三种方法都无法从服务器下载所有图像。很少下载,也很少。

我注意到每个方法都无法从特定位置下载图像。 那就是方法3总是无法下载前三个图像。我更改了图像,但即便如此,前三张图像也没有下载。

方法一:

 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;

   }

这里我得到的丢失图像的错误是 :SKIimagedecoder ,工厂返回 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;
   }

我在这里得到的错误和上面一样。

方法三:

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.NoHttpResponseException:目标服务器未能响应。

请帮忙。这是阻止我完成项目的唯一原因。

【问题讨论】:

  • 它是仅在一台服务器上执行此操作,还是在所有服务器上执行此操作?会不会是服务器端的问题?您是否尝试过从其他位置下载文件,只是为了测试?
  • 不,我没有尝试从其他服务器下载。
  • 请尝试一下。看到你正在尝试 3 种不同的方法(没有检查过,但我假设你有),我的第一个怀疑是事情的另一面。 (或介于两者之间的东西,比如连接)
  • 是的,他们都在工作。我没有其他服务器。
  • @rajivpradeep 那么您应该对答案发表评论,以描述尝试时会发生什么。如果您不与我们合作,没有人可以帮助您

标签: android


【解决方案1】:

看看这个..清楚地解释了从服务器下载图像..Image from Server....

【讨论】:

    【解决方案2】:

    我假设您正在下载要显示的图像,而不仅仅是保存在设备上。如果是这种情况,我建议考虑使用Droid-Fu,更具体地说是WebImageView。您可以将 URL 传递给 WebImageView,它会尽可能地加载图像,这将避免由于连接超时而导致图像无法加载,我猜这是您遇到的问题。

    在 XML 中:

    <com.github.droidfu.widgets.WebImageView
        android:id="@+id/image"
        android:layout_width="70dip"
        android:layout_height="70dip"
        droidfu:autoLoad="true"
        droidfu:progressDrawable="..."
    />
    

    在代码中:

    WebImageView  image = (WebImageView) convertView.findViewById(R.id.image);
    image.setImageUrl(image_url);
    image.loadImage();
    

    【讨论】:

    • 这不是我想做的。
    【解决方案3】:

    您创建位图可能需要一些时间,因此连接会超时。您可能会获得对所有输入流的引用,然后下载并创建。这是一个粗略的答案,但如果推理正确,您可以改进它:

    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;
    
    }
    

    【讨论】:

    • 很可能是的,这就是我面临的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-30
    相关资源
    最近更新 更多