Question

I looked all over stackoverflow and haven't found one solution for this!

    package com.example.sfgsfg;

import java.io.InputStream;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView img = (ImageView) findViewById(R.id.imageView1);
        try {
            URL url = new URL(
                    "http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg");
            // try this url =
            // "http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg"
            HttpGet httpRequest = null;

            httpRequest = new HttpGet(url.toURI());

            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = (HttpResponse) httpclient
                    .execute(httpRequest);

            HttpEntity entity = response.getEntity();
            BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
            InputStream input = b_entity.getContent();

            Bitmap bitmap = BitmapFactory.decodeStream(input);

            img.setImageBitmap(bitmap);

        } catch (Exception ex) {

        }

    }

}

This is my latest try, and I've tried all of those below: Display Image(From Url) In ImageView Android, Make an image at a URL equal to ImageView's image How to load an ImageView by URL in Android? how to set image from url for imageView Android App Display Image From URL

Nothing works! And I need it without using AsyncTask! Please Help!

OTHER TIPS

I highly recommend using this library

Use Glide or Picasso to fetch image from URL and set it to imageview.This two is very famous library usually used.

For Glide implement 'com.github.bumptech.glide:glide:4.2.0' in build.gradle file or in java

Glide.with(getApplicationContext())
                .load(image)
                .into(imageView);

Or

For Picasso implement 'com.squareup.picasso:picasso:2.71828' in build.gradle and in java

Picasso.get()
  .load(url) 
  .resize(50, 50)
  .centerCrop()
  .into(imageView)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top