简体   繁体   中英

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM