4

I am developing an Android app in which I am retrieving an image from a server and show it in an image view using Picasso. Some image URLs don't work even though I can test them successfully in a browser.

For example this URL works correctly:

http://www.tonightfootballreport.com/\Filebucket\Picture\image\png\20160730011032_BPL.png

But this one fails:

http://www.tonightfootballreport.com/\Filebucket\Picture\image\png\20160807025619_Serie A.png

The difference appears to be that the failing URL contains a space. What do I need to do to make this work?

Clyde
  • 7,389
  • 5
  • 31
  • 57
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372

3 Answers3

10
String temp = "http://www.tonightfootballreport.com/\Filebucket\Picture\image\png\20160807025619_Serie A.png";
temp = temp.replaceAll(" ", "%20");
URL sourceUrl = new URL(temp);
Shankar
  • 269
  • 4
  • 17
  • The actual solution is to encode the URL like the other answer suggests. This answer only encodes spaces, but not all other characters which need to be encoded. – Xaver Kapeller Sep 19 '16 at 05:41
  • if your url contains non English words, this answer won't work! – Milad Yarmohammadi Sep 19 '16 at 05:44
  • Hmm... He is asking only spaces. – Shankar Sep 19 '16 at 11:10
  • 1
    That doesn't change that this is a bad answer. He is asking for spaces because he has only encountered the problem with spaces. The op obviously doesn't know about url encoding etc and giving him/her this half solution that appears to work is like a bug timebomb itching to explode. – Xaver Kapeller Sep 19 '16 at 13:13
7

Encode the URL,

String url = "http://www.tonightfootballreport.com/Filebucket/Picture/image/png/20160807025619_Serie A.png";
String encodedUrl = URLEncoder.encode(url, "utf-8");

EDIT #1 :

The problem with above method as @Wai Yan Hein, pointed is that it encode all the characters in the url including the protocol.

The following code solves that issue,

String urlStr = "http://www.tonightfootballreport.com/Filebucket/Picture/image/png/20160807025619_Serie A.png";
URL url = new URL(urlStr);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();

Edit #2

Alternate solution using Uri.parse,

String urlStr = "http://www.tonightfootballreport.com/Filebucket/Picture/image/png/20160807025619_Serie A.png";
String url = Uri.parse(urlStr)
                .buildUpon()
                .build()
                .toString();
K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33
  • 3
    Url become like this, http%3A%2F%2Fwww.tonightfootballreport.com%2F%5CFilebucket%5CPicture%5Cimage%5Cpng%5C20160730011032_BPL.png and all not working. – Wai Yan Hein Sep 19 '16 at 05:35
0

Check if the URL is indeed valid and if not try encoding it,

if(!Patterns.WEB_URL.matcher(url).matches()){
            URLEncoder.encode(url, "utf-8");
            //Now load via Picasso
        }
else{
      //Proceed with loading via picasso
}
Aneeb Khawar
  • 330
  • 1
  • 8