I am trying to get image in ImageView from url website but the image not show so, What is the wrong in this code? This is the url of the image.
It is my Main Activi
Doing network IO in the main thread is evil. Better to avoid.
Also - your url resource access is wrong.
Use something like this instead:
private Bitmap bmp;
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
InputStream in = new URL(IMAGE_URL).openStream();
bmp = BitmapFactory.decodeStream(in);
} catch (Exception e) {
// log error
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if (bmp != null)
imageView.setImageBitmap(bmp);
}
}.execute();
This is the 'old way' of loading url resources into display. Frankly speaking, I have not written such code in a long time. Volley and Picasso simply do it much better than me, including transparent local cache, multiple loader-threads management and enabling effective resize-before-load policies. All but coffee :)
Your problem is not in code but with the server. Try this sample url and tell me if is it working:
http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png
If you're going to be loading multiple images from URL's in your app, it's definitely worth looking into:
nostra13's "Universal Image Loader"
It's an awesome library with tons of features to display images from URLs, cast to bitmaps, etc.
Once you've included the class and declared the imageloader + imageloader configuration, its a simple as this:
imageLoader.displayImage("http://www.yoursite.com/my_picture.png", imageView);
Where imageView is the imageView you would like the image to appear in.
Give it a try with Picasso. It should take you almost one full line of code and less than a minute to set it up.
In your case, that would be:
Picasso.with(context).load(""http://imageurlgoeshere").into(i);