0

Im trying to load an image from url to my android ImageView. but it gives no image for my url. but when i call another sample url it loads on the ImageView

My URL which gives empty

https://192.168.100.15/HeyVoteWeb/Home/GetImage/d9cbd32c-47fc-4644-ab97-1f525c96e9ed/100000102

This sample URL works for me

https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png

This is the code i am working on

public class GetImage extends Activity{
ImageView postpic1;
Bitmap b;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.all_posts);
    postpic1 = (ImageView) findViewById(R.id.postpic1);
    information info = new information();
    info.execute("");
}
public class information extends AsyncTask<String, String, String>
{
    @Override
    protected String doInBackground(String... arg0) {
        try
        {
            URL url = new URL("https://localhost/HeyVoteWeb/Home/GetImage/d9cbd32c-47fc-4644-ab97-1f525c96e9ed/100000102");
            InputStream is = new BufferedInputStream(url.openStream());
            b = BitmapFactory.decodeStream(is);
        } catch(Exception e){}
        return null;
    }
    @Override
    protected void onPostExecute(String result) {
        postpic1.setImageBitmap(b);

    }
}
}
Naz141
  • 433
  • 1
  • 8
  • 31

5 Answers5

5

The URL for your image is localhost. Localhost(127.0.0.1) refers to same machine as the request origination. So, your phone sends request to itself. Instead specify the IP address of your pc where the server is running.

PS: Ensure both your PC and your phone are connected to the same network.

Srinivas
  • 332
  • 4
  • 18
  • @Naz141: Have you setup SSL on your server? – Srinivas Nov 03 '15 at 13:39
  • when i run this url in the web browser, a window will popup to download the image from url. it will not show the image in the browser – Naz141 Nov 03 '15 at 13:43
  • @Naz141: http://stackoverflow.com/questions/4221874/how-do-i-allow-https-for-apache-on-localhost to read further on how to set it up. Instead can you try with plain http? – Srinivas Nov 03 '15 at 13:44
  • @Naz141: Can you see the image from your browser on android? If not, check if both your phone and server are on the same network. – Srinivas Nov 03 '15 at 13:45
  • yes i can see in my phone and the image auto download when run in browser – Naz141 Nov 03 '15 at 13:52
  • @Naz141: Without further information on your environment, it will be difficult to help. – Srinivas Nov 03 '15 at 13:58
  • Of which device is the IP 192.168.100.15 ? – LilaQ Nov 03 '15 at 14:26
  • @LilaQ: From his response, it is evident that his server has the IP mentioned, since the image is getting served for the same IP. – Srinivas Nov 03 '15 at 14:49
  • I'm not even sure he knows that/if he has a server running, he did not answer to any of these direct questions on this whole post. – LilaQ Nov 03 '15 at 14:54
2

I think that your problem is in your URL, replace your localhost with your IP address, hope it solves your problem.

KRist
  • 1,402
  • 12
  • 10
1

Just use image loading and caching libraries. For instance Picasso

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

Alternative solution is Glide; It has a similar working principleIt has a similar working principle:

Glide.with(this).load("http://goo.gl/gEgYUd").into(imageView);
ArtKorchagin
  • 4,801
  • 13
  • 42
  • 58
1

Do you have a local webserver running which supports HTTPS? Because that is where you are trying to load an image from.

Also, if you have on running, do you get the image when you call your desired URL in your browser?

LilaQ
  • 349
  • 5
  • 19
  • this is the url `https://192.168.100.15/HeyVoteWeb/Home/GetImage/d9cbd32c-47fc-4644-ab97-1f525c96e9ed/100000102` – Naz141 Nov 03 '15 at 13:32
  • Yes, I saw that. That is a call to a local HTTPS webserver, so do you have one running? If not, there is no way this URL will ever give you an image. This calls to the computer / device you are running this on, **not** to a random image on the internet (just for clarification). – LilaQ Nov 03 '15 at 13:33
  • 1
    I can not call this URL, because I don't have a running webserver on my computer, with a directory path e.g. C:\xampp\htdocs\HeyVoteWeb\Home\GetImage\d9cbd32c-47fc-4644-ab97-1f525c9‌​6e9ed\100000102 ---- Where did you get that link from? – LilaQ Nov 03 '15 at 13:36
0

Have you tried using Picasso library very easy and effective:

  1. Go to you build.gradle inside app dir and add to dependencies:

    compile 'com.squareup.picasso:picasso:2.5.2'

  2. Then use picasso lib:

    String url = "https://localhost/HeyVoteWeb/Home/GetImage/d9cbd32c-47fc-4644-ab97-1f525c96e9ed/100000102";
    

    Picasso.with(context) //The context of your activity .load(url) .into(postpic1);

CommonSenseCode
  • 23,522
  • 33
  • 131
  • 186
  • when i put my below url it does not load the imageview is empty. but it work for all other image url `https://192.168.100.15/HeyVoteWeb/Home/GetImage/d9cbd32c-47fc-4644-ab97-1f525c96e9ed/100000102` – Naz141 Nov 03 '15 at 13:49
  • my image url work i my browser. the image gets auto downloaded when i run the url in browser – Naz141 Nov 03 '15 at 13:50
  • @Naz141, then that's the problem you should be using a link to the img directly not a link to download the img they are 2 different links with 2 different functionalities – CommonSenseCode Nov 03 '15 at 13:54