0

I am trying to download image from server. Few are downloading and few and creating problem. I don't know why.

I have downloaded and show image to user on the same location. Here is the file which is able to download.

http://www.mongreldog.co.nz/unilogo/Backgrounds_20399.png

When I am trying to download following image. This image is opening in browser but not downloading in android

http://www.mongreldog.co.nz/unilogo/Twitter-Ryan_Giggs_Imogen_Thomas_Guard-Footballer_Affair_UK_Manchester%20United_M_785.jpg

Its give exception

java.io.FileNotFoundException: http://www.mongreldog.co.nz/unilogo/Twitter-Ryan_Giggs_Imogen_Thomas_Guard-Footballer_Affair_UK_Manchester United_M_785.jpg at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:521) at src.com.mongreldog.appsupport.Utils.downloadImage(Utils.java:77) at src.com.mongreldog.ViewFullCompAndCommentActivity$3.performInBackground(ViewFullCompAndCommentActivity.java:607) at src.com.mongreldog.appsupport.HeavyWorker.doInBackground(HeavyWorker.java:44) at src.com.mongreldog.appsupport.HeavyWorker.doInBackground(HeavyWorker.java:1) at android.os.AsyncTask$2.call(AsyncTask.java:185)

Here is my code.

public static Bitmap downloadImage(String imageURLStr) {


        Bitmap bitmap = null;
        InputStream in = null;
        try {
            URL url = new URL(imageURLStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            in = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        } catch (SocketTimeoutException e) {
            bitmap = null;
            e.printStackTrace();
        } catch (IOException e) {
            bitmap = null;
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            bitmap = null;
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            bitmap = null;
        }
        return bitmap;

}
Arslan Anwar
  • 18,746
  • 19
  • 76
  • 105

4 Answers4

1

Use URL Encoder to encode the image URL, as you can see the URL have white spaces in the log report.

public static Bitmap downloadImage(String imageURLStr) {
      imageURLStr  = URLEncoder.encode(imageURLStr, "utf-8");
      //... rest of your code.
}

Edit: as you reported of issue '+' instead of %20

you can use

public static Bitmap downloadImage(String imageURLStr) {
          imageURLStr  = imageURLStr.replaceAll(" ", "%20");
          //... rest of your code.
}

For source check here

Community
  • 1
  • 1
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
  • you are right there are specs. I am trying to use your code but it replace the " " with "+". But it should be "%20". – Arslan Anwar Nov 04 '11 at 12:20
  • 1
    Thanks for you answer. I just try "Uri.encode()" by Nabeel answer. And its working. The above solution for replaceAll() is not look goods to me. – Arslan Anwar Nov 04 '11 at 12:35
1

I try to use URLEncoder.encode() to encode the URL. Its strange that it convert " " with "+".

Please try Uri.encode(imageURL). I just try it and its working perfectly.

I have tested that in android.

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
Muhammad Nabeel Arif
  • 19,140
  • 8
  • 51
  • 70
0

Looks like the file name is not being parsed correctly (the space between 'Manchester' and 'United').

Use URLEncoder.encode() to encode the URL.

dotty
  • 40,405
  • 66
  • 150
  • 195
0

Your second URL is very instable. It may return 404 in the most cases even in Chome. I have seen the picture only once.

FeelGood
  • 5,594
  • 2
  • 25
  • 22