下载图片网址包含"è"

ePe*_*ace 5 android image

我尝试下载以下网址中的图片:

http://upload.tapcrowd.com//cache/ /_cp_100_100_stand_filière_300x212.jpg

正如您在浏览器中看到的那样,它显示了一个图像,但在我的应用程序中,我得到了一个FileNotFoundException.

但是,如果我将图像的网址从"è"更改为"e".我可以成功将其下载到我的应用程序中.然而,这只是一个临时解决方案,因为它需要能够使用unicode标志下载图像.

我怎样才能做到这一点?

用于下载图像的方法:

        Bitmap bitmap = null;
        URL imageUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
        conn.setConnectTimeout(30000);
        conn.setReadTimeout(30000);
        conn.setInstanceFollowRedirects(true);
        InputStream is = conn.getInputStream();
        OutputStream os = new FileOutputStream(f);
        Utils.CopyStream(is, os);
        os.close();
        bitmap = decodeFile(f, maxheight, maxwidth);
Run Code Online (Sandbox Code Playgroud)

结果代码对我有用:

        Bitmap bitmap = null;
        int slashIndex = url.lastIndexOf('/');
        String filename = url.substring(slashIndex + 1);
        filename = URLEncoder.encode(filename, "UTF-8");
        url = url.subSequence(0, slashIndex + 1) + filename;
        URL imageUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
        conn.setConnectTimeout(30000);
        conn.setReadTimeout(30000);
        conn.setInstanceFollowRedirects(true);
        InputStream is = conn.getInputStream();
        OutputStream os = new FileOutputStream(f);
        Utils.CopyStream(is, os);
        os.close();
        bitmap = decodeFile(f, maxheight, maxwidth);
Run Code Online (Sandbox Code Playgroud)

ben*_*n75 3

使用URLEncoder对 url 进行编码:

\n\n
String baseUrl = "http://upload.tapcrowd.com//cache//";\nString imageName = "_cp_100_100_stand_fili\xc3\xa8re_300x212.jpg";\nURL imageUrl = new URL(baseUrl+URLEncoder.encode(imageName ,"UTF-8"));\n
Run Code Online (Sandbox Code Playgroud)\n\n

它适用于您的浏览器,因为浏览器足够智能,可以在您在网址栏中输入重音时进行编码。

\n