2

I have a problem downloading an image from Google Cloud Storage public url from my android app. My code works perfectly for images from other sources. The images from Google Cloud Storage can also be accessed from a web browser without problems. Take this image for example: http://tin_images.storage.googleapis.com/1420678062-zmj1qJQe126843HbyJvbUI.jpg

Here is an intentservice which demonstrates the problem:

public class TestService1 extends IntentService {

public TestService1(){
    super("TestService1");
}

@Override
protected void onHandleIntent(Intent intent) {
    String urlString="http://tin_images.storage.googleapis.com/1420678062-zmj1qJQe126843HbyJvbUI.jpg";
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(urlString);
        HttpResponse response = httpclient.execute(httpget);
    }
    catch(IOException e){
        e.printStackTrace();
    }

}
}

This gives me the following error message:
java.lang.IllegalArgumentException: Host name may not be null

I have also tried using httpurlconnection:

public class TestService2 extends IntentService {

public TestService2() {
    super("TestService2");
}

@Override
protected void onHandleIntent(Intent intent) {
    String urlString="http://tin_images.storage.googleapis.com/1420678062-zmj1qJQe126843HbyJvbUI.jpg";
    try {
        URL url = new URL(urlString);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.connect();
    }
    catch(IOException e){
        e.printStackTrace();
    }
}
}

The error here (which is caught in the catch-block) is
java.net.UnknownHostException: http://tin_images.storage.googleapis.com/1420678062-zmj1qJQe126843HbyJvbUI.jpg

In both of these cases it works fine if I use another url. I prefer using HttpClient as I use that library in the rest of the app.

Is there any way to solve this problem?

Fredrik Wallén
  • 438
  • 5
  • 13
  • Take a look here: http://stackoverflow.com/questions/15121407/solvedhost-name-may-not-be-null-error-in-android and check the httpclient version you are using. – tato.rodrigo Jan 12 '15 at 16:47
  • I changed the name of my bucket on Google Cloud Storage from tin_images to tin-images and it seems to be working, which means the underscore was the source of the problem. Thank you! Add an answer and I will accept it. – Fredrik Wallén Jan 12 '15 at 17:19

2 Answers2

2

As stated here, HttpClient seems not to support underscores for hostname.

Also, there is a long discussion about the use of underscores on domain names and hostnames.

Community
  • 1
  • 1
tato.rodrigo
  • 2,783
  • 21
  • 26
0

Didn't you forget about this:

android.permission.INTERNET
android.permission.ACCESS_NETWORK_STATE
android.permission.READ_PHONE_STATE

?

Also try to open this image from default web browser application.

I'd recommend you to download image with universal image loader https://github.com/nostra13/Android-Universal-Image-Loader

QArea
  • 4,955
  • 1
  • 12
  • 22