0

I am trying to load an image from a server to show it in an ImageView

I used

    ImageView imgView = (ImageView) findViewById(R.id.ivProduct);

    Bitmap bitmap = null;

    try {

        URL urlImage = new URL(
                "http://www.google.fr/intl/en_com/images/srpr/logo1w.png");
        HttpURLConnection connection = (HttpURLConnection) urlImage
                .openConnection();
        InputStream inputStream = connection.getInputStream();
        bitmap = BitmapFactory.decodeStream(inputStream);
        imgView.setImageBitmap(bitmap);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}`

This worked fine but when I downloaded the same image on my server and I changed the url to

http://localhost:9527/market_helper/img_products/logo1w.png

It did not work. What is the problem ?

Aymen Taarit
  • 672
  • 1
  • 10
  • 22
  • 1
    Are you sure you are on the same network. I guess that this is a network problem which has nothing to do with Android –  Dec 11 '11 at 16:15
  • when I type this http://localhost:9527/market_helper/img_products/logo1w.png I can see the image from my browser – Aymen Taarit Dec 11 '11 at 16:23
  • Are you testing your code on a device or a simulator? –  Dec 11 '11 at 16:26
  • the url may not be correct first check if connection is ok the go forward – Harsh Dev Chandel Dec 11 '11 at 16:45
  • Hi Aymen Taarit, Have you solved it. I am also facing the same problem, when I use the url like http://server_ip/image.jpeg but no result. I have few doubts, Can I use url without port number ?, do I need to install web server on my server pc ?. Thanks in advance.... – Haris Jun 11 '13 at 11:04

2 Answers2

3

The problem is that in your url the http://localhost:9527 says it is running on a server on your local machine, but when accessing from your Android the http://localhost refers to the device itself.

If you are on the same network you can try access it by replacing the localhost part with your PC's local IP address (for example 192.168.100.6) You can find out what your IP is by typing ipconfig in the command line.

Shashanth
  • 4,995
  • 7
  • 41
  • 51
Mats Hofman
  • 7,060
  • 6
  • 33
  • 48
0

localhost is the loopback adapter on the machine (127.0.0.1) you will probably not be able to use this address from the android app (might work with simulator but I still wouldn't use it).

If you are on the same network, the machine name might work but it depends on what you are using to host the image. If you are using a full blown web server like IIS or something like that you should be good (don't forget to check firewall settings on the server to all incoming connections on the port you use). If you are using something like the VS web server (cassini) then it will not work because it doesn't allow connections from off the box.

If you are not on the same network (like the phone is using cell data) then you will need something publicly addressable (a DNS name or IP that points to your server on the internet). You don't specify what you are using on the server, but there are many hosting solutions out there for free or very cheap that you could use.

Hope this helps!

Brian ONeil
  • 4,229
  • 2
  • 23
  • 25