tom91136
tom91136

Reputation: 8962

Android url get file fail

I've got a url:

http://www.mubie.com/sites/default/files/imageifarm/紫薇.jpg

now, if I try to get the file:

String url = "http://www.mubie.com/sites/default/files/imageifarm/紫薇.jpg"; //url is a string
Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(url).getContent());

it throws the following exception:

09-23 17:10:57.812: W/System.err(2059): java.io.FileNotFoundException: http://www.mubie.com/sites/default/files/imageifarm/紫薇.jpg
09-23 17:10:57.812: W/System.err(2059):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
09-23 17:10:57.812: W/System.err(2059):     at java.net.URLConnection$DefaultContentHandler.getContent(URLConnection.java:1042)
09-23 17:10:57.812: W/System.err(2059):     at java.net.URLConnection.getContent(URLConnection.java:203)
09-23 17:10:57.812: W/System.err(2059):     at java.net.URL.getContent(URL.java:447)
09-23 17:10:57.822: W/System.err(2059):     at com.sora.insync.demo.ViewQuestion.loadImageFromNetwork(ViewQuestion.java:273)

how can i solve this issue? convert the chinese characters to unicode?

The url(and the file) is perfectly valid

what i've tried

Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(URLEncoder.encode(url,
                "UTF8")).getContent());

but it turns the url into something like:

http%3A%2F%2Fwww.mubie.com%2Fsites%2Fdefault%2Ffiles%2Fimageifarm%2F%E7%B4%AB%E8%96%87.jpg

Upvotes: 0

Views: 643

Answers (1)

tolgap
tolgap

Reputation: 9778

As you can see, your code is failing on the Chinese characters. You should call toASCIIString() on your URL-string:

InputStream stream = (InputStream) new URL(url).toASCIIString().getContent();
Bitmap bitmap = BitmapFactory.decodeStream(stream);

Upvotes: 1

Related Questions