Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Picasso not working if url contains space

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?

like image 794
Wai Yan Hein Avatar asked Dec 03 '22 14:12

Wai Yan Hein


2 Answers

String temp = "http://www.tonightfootballreport.com/\Filebucket\Picture\image\png\20160807025619_Serie A.png";
temp = temp.replaceAll(" ", "%20");
URL sourceUrl = new URL(temp);
like image 60
Shankar Avatar answered Dec 06 '22 12:12

Shankar


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();
like image 31
K Neeraj Lal Avatar answered Dec 06 '22 12:12

K Neeraj Lal