如何在URL中处理UTF-8编码的韩文字符,以便使用Android URL对象下载图像

时间:2013-08-16 08:00:23

标签: android url utf-8 ioexception


目标

通过包含韩文字符(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代码,正确的版本已经放到了目标部分。

2 个答案:

答案 0 :(得分:1)

您可以先使用以下方法解码您的网址:

String values = URLDecoder.decode(url,“UTF-8”);

然后,使用该字符串。

答案 1 :(得分: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");