0

我有一个网址:

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?

url(和文件)完全有效

我试过的

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
4

1 回答 1

1

如您所见,您的代码在中文字符上失败了。您应该调用toASCIIString()您的 URL 字符串:

InputStream stream = (InputStream) new URL(url).toASCIIString().getContent();
Bitmap bitmap = BitmapFactory.decodeStream(stream);
于 2012-09-23T09:26:03.243 回答