2013-06-03 181 views
0

我在通過電子郵件命名的服務器上有圖像,所以當我嘗試在應用程序中下載它們時,@符號未顯示在路徑上,是否有阻止轉義此符號的方法?從網絡下載圖片

例子: 正道 Http://www.xxxxx.com/[email protected]

歧途 Http://www.xxxxx.com/aa.com.jpg

我試圖URL編碼,但它不是有用的在我的情況

Bitmap downloadFile(String fileUrl) { 
    URL myFileUrl = null; 
    Bitmap bmImg = null; 
    try { 
     myFileUrl = new URL(fileUrl); 
    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    try { 
     HttpURLConnection conn = (HttpURLConnection) myFileUrl 
       .openConnection(); 
     conn.setDoInput(true); 
     conn.connect(); 
     InputStream is = conn.getInputStream(); 

     bmImg = BitmapFactory.decodeStream(is); 

     // imView.setImageBitmap(bmImg); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return bmImg; 

} 
+1

分享您如何生成URI的代碼。 – cgTag

+0

@MathewFoscarini我更新了這個問題,我想我的代碼是正確的,但是URL沒有接受@符號,所以我的圖片url變成了錯誤。 – user2448444

+0

看看這個:http://stackoverflow.com/questions/541966/how-do-i-do-a-lazy-load-of-images-in-listview – cgTag

回答

0

使用URLEncoder從Android SDK中嘗試。

try { 
    myFileUrl = new URL(java.net.URLEncoder.encode(fileUrl)); 
} catch (MalformedURLException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

編輯:

剛剛看到我的錯誤。這是行不通的,因爲它是對整個URL進行編碼。

您必須將電子郵件地址與其他網址分開。

myFileUrl = new URL("http://www.xxxx.com/"+java.net.URLEncoder.encode(email)); 

或者,只需更換問題字符。

myFileUrl = new URL(fileUrl.replace("@","%40")); 
+0

我試過,因爲我提到 – user2448444

+0

The服務器迴應404錯誤? – cgTag

+0

@ user2448444更新了我的答案,我犯了一個錯誤。 – cgTag