2011-04-28 108 views
0

下载图像我用这样的代码从URL下载图像:问题与URL

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

     try { 
      in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE); 

      final ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); 
      out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE); 
      copy(in, out); 
      out.flush(); 

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

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

     return bitmap; 
    } 

当我发送URL“http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009 /04/android_icon_256.png“它工作正常,但是当我使用”http://www.hospimedica.com/images/stories/articles/article_images/_CC/20110328%20-%20DJB146.gif“它返回我空。 这个URL有什么问题?

回答

0

为什么要编写自己的方法来下载图片? Android有内置的方法来实现这一点..只需使用

URL url = new URL("Your url"); 
Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
+0

它不起作用。网址有问题,但我不知道究竟是什么 – shtkuh 2011-04-28 12:11:03

+0

也许是因为URL中的空格? – shtkuh 2011-04-28 12:19:49

+0

你会得到什么错误? – thoredge 2011-04-28 12:31:47