Anh Duy
Anh Duy

Reputation: 1183

Android with plus sign ("+") in url

I cannot download the picture: http://www.wallpick.com/wp-content/uploads/2014/02/08/Water+Sports_wallpapers_242-640x480.jpg

This is my code:

// from web

    try {
        Bitmap bitmap = null;

        URL imageUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) imageUrl
                .openConnection();
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setConnectTimeout(25000);
        conn.setReadTimeout(25000);
        conn.setInstanceFollowRedirects(true);
        InputStream is = conn.getInputStream();
        OutputStream os = new FileOutputStream(f);
        // save file to m_FileCache
        copyStream(is, os);
        os.close();
        bitmap = decodeFile(f);
        return bitmap;
    } catch (Throwable ex) {
        return null;
    }

With this code, I can download all image urls as:

http://www.wallpick.com/wp-content/uploads/2014/02/08/pictures-of-lotus-flowers-on-water-640x480.jpg

Root cause is plus sign ("+") in first link. Please help me! Thank you very much!

Upvotes: 1

Views: 1177

Answers (1)

Sergi Pasoevi
Sergi Pasoevi

Reputation: 2851

You can use Uri builder classes. As an example,

String url = Uri.parse("http://www.wallpick.com/wp-content/uploads/2014/02/08/").buildUpon()
            .appendEncodedPath("Water+Sports_wallpapers_242-640x480.jpg")
            .build().toString();

This will correctly encode your url String.

Upvotes: 1

Related Questions