0

目标

通过包含韩文字符(UTF-8 编码)的 URL 字符串下载图像。


问题

在以下代码中,String urlString带有一个字符串,其中包含例如韩语字符콘(其 UTF-8 代码为 %EC%BD%98):

http://domain.com/image/콘test.png

在此语句中捕获了 IOException:

bitmap = BitmapFactory.decodeStream(urlObject.openConnection().getInputStream()); // try catch IOException


代码

public void loadImageWithUrlString(ImageView imageView, String urlString) {
        URL urlObject;
            try {
                urlObject = new URL(urlString); // try catch MalformedURLException
                Bitmap bitmap;
                bitmap = BitmapFactory.decodeStream(urlObject.openConnection().getInputStream()); // try catch IOException
                imageView.setImageBitmap(bitmap);
            } catch (MalformedURLException e) {
                Log.d("congliu", "loadImageWithUrlString() : oops this url is caught a MalformedURLException " + urlString);
                imageView.setImageResource(R.drawable.default_image);
                e.printStackTrace();
            } catch (IOException e) {
                Log.d("congliu", "loadImageWithUrlString() : oops this url is caught a IOException " + urlString);
                imageView.setImageResource(R.drawable.default_image);
                e.printStackTrace();
            }
    }

更新 - 错误消息

08-16 16:51:59.194: W/System.err(6911): java.io.FileNotFoundException: http://domain.com/image/우test.png
08-16 16:51:59.194: W/System.err(6911):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:521)
08-16 16:51:59.194: W/System.err(6911):     at com.domain.android.MainActivity.loadImageWithUrlString(MainActivity.java:1288)
08-16 16:51:59.194: W/System.err(6911):     at com.domain.android.MainActivity$13$1.run(MainActivity.java:926)
08-16 16:51:59.194: W/System.err(6911):     at android.os.Handler.handleCallback(Handler.java:587)
08-16 16:51:59.194: W/System.err(6911):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-16 16:51:59.194: W/System.err(6911):     at android.os.Looper.loop(Looper.java:130)
08-16 16:51:59.194: W/System.err(6911):     at android.app.ActivityThread.main(ActivityThread.java:3691)
08-16 16:51:59.194: W/System.err(6911):     at java.lang.reflect.Method.invokeNative(Native Method)
08-16 16:51:59.194: W/System.err(6911):     at java.lang.reflect.Method.invoke(Method.java:507)
08-16 16:51:59.194: W/System.err(6911):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
08-16 16:51:59.194: W/System.err(6911):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
08-16 16:51:59.194: W/System.err(6911):     at dalvik.system.NativeStart.main(Native Method)

更新 - UTF-8

抱歉,我误解了 UTF-8 代码,正确的版本已放在目标部分。

4

2 回答 2

1

也许您可以先使用以下方法解码您的 URL:

字符串值 = URLDecoder.decode(url, "UTF-8");

然后,使用该字符串。

于 2013-08-16T08:10:16.690 回答
1

好的,我有两个选择,每个尝试

...
InputStream is=urlObject.openConnection().getInputStream();
is.setEncoding("ISO-8859-1"); // try "UTF-8" if this doesn't work
bitmap = BitmapFactory.decodeStream(is);
...

或者

String imageURL = "우test.png";
String host = "http://domain.com/image/";
String encodedUrl = host + UrlEncoder.encode(imageURL ,"utf-8");
于 2013-08-16T08:55:07.770 回答