Question

I'm having trouble loading pictures which contain Latin characters like: č, ć, š, đ, ž. The code works flawlessly for other links, but when it gets to this or any other containing Latin character:

InputStream input = null;
            try {
                URL url = new URL(http://www.novosti.rs/upload/thumbs/images/2012//09/28j/Supruga%20Gorana%20Savića_75x45.jpg);
                HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                conn.setDoInput(true);
                conn.connect();
                input = conn.getInputStream();
                Bitmap myBitmap = BitmapFactory.decodeStream(input);
                conn.disconnect();
                return myBitmap;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            } finally {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

It throws a:

java.io.FileNotFoundException: http://www.novosti.rs/upload/thumbs/images/2012//09/28j/Supruga%20Gorana%20Savića_75x45.jpg

although you can try copying the link to your address bar to see it's valid. So what can I do?

Was it helpful?

Solution

Try using URLEncoder.encode(String s, String charsetName) which will convert the special characters to a % delimited numeric form.

See URLEncoder documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top