Goal
To download image through an URL string that contains Korean characters (UTF-8 encoded).
Problem
In the following code, the String urlString
carries a string that contains, for example, a Korean character 콘 (whose UTF-8 code is %EC%BD%98):
http://domain.com/image/콘test.png
An IOException is caught at this statement:
bitmap = BitmapFactory.decodeStream(urlObject.openConnection().getInputStream()); // try catch IOException
Code
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();
}
}
Update - Error Messages
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)
Update - UTF-8
Sorry that I misunderstand the UTF-8 code, the correct version has been to put to Goal section.