Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download image URL contains "è"

Tags:

android

image

I try to download the image in the following url:

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

As you can see in the browser this shows an image, but in my app I get a FileNotFoundException.

However if i change the url of the image from "è" to "e". I can succesfully download it into my app. This however is only a temporary solution as it needs to be able to download images with unicode sign.

How can I achieve this?

Method used to download images:

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

result code that works for me:

        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);
like image 546
ePeace Avatar asked Oct 21 '22 18:10

ePeace


1 Answers

Encode the url using URLEncoder:

String baseUrl = "http://upload.tapcrowd.com//cache//";
String imageName = "_cp_100_100_stand_filière_300x212.jpg";
URL imageUrl = new URL(baseUrl+URLEncoder.encode(imageName ,"UTF-8"));

It works with your browser, because browser is smart enough to do the encoding when you type accent in your url bar.

like image 71
ben75 Avatar answered Oct 27 '22 09:10

ben75