Reputation: 51
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!
Upvotes: 0
Views: 488
Reputation: 324
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)
Upvotes: 0
Reputation: 2891
you can use one of the following links:
http://sunil-android.blogspot.in/2013/09/lazy-loading-image-download-from.html
Upvotes: 1