2

I am trying to display an image in android by receiving the url from the web server and then turning it to a bitmap image but getting the following error as the symbol %5C is in it.

E/Error﹕ http:%5C/%5C/thumbs3.ebaystatic.com%5C/pict%5C/3007385805144040_5.jpg

I have tried url2.replaceAll("%5C",""); to get rid of the symbol but this has no effect at all. How can I get rid of it so I have a valid url.

user2052514
  • 467
  • 1
  • 4
  • 9
  • 2
    Did you not assign the result of calling `replaceAll` back to `str2`? Strings are immutable in Java - just calling `replaceAll` and ignoring the result will have no effect. Having said that, I would take a close look at *why* you're getting that data to start with - there may well be better approaches. – Jon Skeet Feb 25 '15 at 15:35
  • You need to assign the result to the same String – Chetan Kinger Feb 25 '15 at 15:37
  • like this String newUrl = url.replaceAll("%5C", ""); – theapache64 Feb 25 '15 at 15:39

2 Answers2

6

What you are looking for is a called URL Decoding.

Read more here: How to do URL decoding in Java?

Don't try manually replacing yourself, use a library or write your own for all cases:

import java.net.URLDecoder;

String result = URLDecoder.decode(url, "UTF-8");

Java 1.7+:

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

String result = URLDecoder.decode(url, StandardCharsets.UTF_8.name());
Community
  • 1
  • 1
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
1
String url = "http:%5C/%5C/thumbs3.ebaystatic.com%5C/pict%5C/3007385805144040_5.jpg";
        String newUrl = url.replaceAll("%5C", "");

Now try the newUrl as your path. If the problem is with URL then this will help you, otherwise the problem coming from when you decode the url to bitmap.

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
theapache64
  • 10,926
  • 9
  • 65
  • 108