0

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!

Community
  • 1
  • 1
  • without asynctask u wil get anr error – KOTIOS Mar 31 '14 at 17:03
  • @evgeny Goldin you want to download the images and want to show in image view or do you want lazy loading? Just for the clarification. – rupesh Mar 31 '14 at 17:04
  • You are probably getting a Networking on main thread exception. You must not use the main thread to perform network tasks, use an AsyntTask or a Thread to load the image from the net and then just do the call to setImageBitmap on the main thread. However unless this is just as a learning exercise , you should probably use of the many excellent libraries that do all this for you *check the answer by Jorge Alfaro) – Bob Mar 31 '14 at 17:09
  • I need somehow to get a Bitmap from an URL, so later on I can set it on the ImageView. I don't mind if it uses thread, but not asynctask! And I need it without using any external jars.. – Evgeny Goldin Mar 31 '14 at 17:38
  • URL newurl = new URL(photo_url_str); mIcon_val = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream()); profile_photo.setImageBitmap(mIcon_val); This code looks so simple.. Why doesn't it work? – Evgeny Goldin Mar 31 '14 at 17:50

3 Answers3

1

I highly recommend using this library

Jorge Alfaro
  • 924
  • 8
  • 11
  • I need somehow to get a Bitmap from an URL, so later on I can set it on the ImageView. I don't mind if it uses thread, but not asynctask! And I need it without using any external jars.. – Evgeny Goldin Mar 31 '14 at 17:39
0

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)
Tanmay Ranjan
  • 318
  • 2
  • 8