我有一个网址:
http://www.mubie.com/sites/default/files/imageifarm/紫薇.jpg
现在,如果我尝试获取文件:
String url = "http://www.mubie.com/sites/default/files/imageifarm/紫薇.jpg"; //url is a string
Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(url).getContent());
它抛出以下异常:
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)
我该如何解决这个问题?将中文字符转换为unicode? p>
网址(和文件)完全有效
我尝试了什么
Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(URLEncoder.encode(url,
"UTF8")).getContent());
但它将网址转换为:
http%3A%2F%2Fwww.mubie.com%2Fsites%2Fdefault%2Ffiles%2Fimageifarm%2F%E7%B4%AB%E8%96%87.jpg
答案 0 :(得分:1)
正如您所看到的,您的代码在中文字符上失败了。您应该在您的URL字符串上调用toASCIIString()
:
InputStream stream = (InputStream) new URL(url).toASCIIString().getContent();
Bitmap bitmap = BitmapFactory.decodeStream(stream);