Android中带有加号(“+”)的url

时间:2014-04-03 13:46:45

标签: android

我无法下载图片: http://www.wallpick.com/wp-content/uploads/2014/02/08/Water+Sports_wallpapers_242-640x480.jpg

这是我的代码:

//来自网络

    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;
    }

使用此代码,我可以将所有图片网址下载为:

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

根本原因是第一个链接中的加号(" +")。请帮我!非常感谢你!

1 个答案:

答案 0 :(得分:1)

您可以使用Uri构建器类。例如,

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

这将正确编码您的网址字符串。