0

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.

George
  • 3,384
  • 5
  • 40
  • 64

2 Answers2

1

May be you can first decode your URL using following method:

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

Then, use that string.

Sushil
  • 8,250
  • 3
  • 39
  • 71
  • Oh your post is helpful but what is needed to do is not `URLDecoder.decode()` but `URLEncoder.encode()`! Sorry that because I didn't understand the situation well. The `urlString` cannot be recognized by `BitmapFactory.decodeStream(urlObject.openConnection().getInputStream())` before doing an explicit `URLEncoder.encode()` in the UTF-8 way and replace all the spaces with `+` (reference is here: http://stackoverflow.com/questions/4737841/urlencoder-not-able-to-translate-space-character) – George Aug 16 '13 at 09:28
1

Ok i have two options, try each

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

or

String imageURL = "우test.png";
String host = "http://domain.com/image/";
String encodedUrl = host + UrlEncoder.encode(imageURL ,"utf-8");
Onur A.
  • 3,007
  • 3
  • 22
  • 37
  • Your second works just as my friend's solution! And since there'll be some spaces in between (sorry I didn't tell in the Question section) so we need to replace spaces with `+` (reference: http://stackoverflow.com/questions/6045377/how-to-insert-20-in-place-of-space-in-android) – George Aug 16 '13 at 09:30
  • For the first solution, @OnurA., are you sure that setEncoding() is method of InputStream? I can't find it (http://developer.android.com/reference/java/io/InputStream.html) nor does compiler knows it. – George Aug 16 '13 at 09:47
  • ah sorry, my bad, it was a method of InputSource but you can easily convert inputstream to inputsource InputSource iss=new InputSource(is); – Onur A. Aug 16 '13 at 10:12