2014-04-03 18 views
0

我無法下載圖片: http://www.wallpick.com/wp-content/uploads/2014/02/08/Water+Sports_wallpapers_242-640x480.jpg的Android與加在URL號( 「+」)

這是我的代碼:

//從網頁

try { 
     Bitmap bitmap = null; 

     URL imageUrl = new URL(url); 
     HttpURLConnection conn = (HttpURLConnection) imageUrl 
       .openConnection(); 
     conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
     conn.setConnectTimeout(25000); 
     conn.setReadTimeout(25000); 
     conn.setInstanceFollowRedirects(true); 
     InputStream is = conn.getInputStream(); 
     OutputStream os = new FileOutputStream(f); 
     // save file to m_FileCache 
     copyStream(is, os); 
     os.close(); 
     bitmap = decodeFile(f); 
     return bitmap; 
    } catch (Throwable ex) { 
     return null; 
    } 

有了這個代碼,我可以下載所有圖片網址爲:

http://www.wallpick.com/wp-content/uploads/2014/02/08/pictures-of-lotus-flowers-on-water-640x480.jpg

根本原因是第一個鏈接中的加號(「+」)。請幫幫我!非常感謝你!

+0

將加號替換爲關聯的HTML號碼:'+' – nKn

+1

這已經回答了:http://stackoverflow.com/a/8962879/29505 – cbrulak

+1

[Android中的Url編碼](http:///stackoverflow.com/questions/3286067/url-encoding-in-android) –

回答

1

您可以使用Uri構建器類。作爲一個例子,

String url = Uri.parse("http://www.wallpick.com/wp-content/uploads/2014/02/08/").buildUpon() 
      .appendEncodedPath("Water+Sports_wallpapers_242-640x480.jpg") 
      .build().toString(); 

這將正確編碼您的url字符串。

+0

感謝您的回答!此鏈接已在服務器中刪除。 –

+0

您是否添加了錯誤檢查功能? –