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