2011-04-28 107 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