AntonioSanchez
AntonioSanchez

Reputation: 327

Android SDK, Set ImageView bitmap from URL

How can I put an image from an url into an ImageView?

i dont want to use any library, id rather stick to the plain old way, i like it. dont judge me. like i said before i got this working but oddly its not anymore

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageView damnit = (ImageView)findViewById(R.id.imageview);
            try {
                URL url = new URL("https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png");

                InputStream in = url.openStream();
                Bitmap bitt = BitmapFactory.decodeStream(in);
                damnit.setImageBitmap(bitt);
            } catch (Exception e) {
                e.printStackTrace();
            }
}

edit!

Im such an idiot, sorry for taking your time guys... I have to have a runOnUI Thread, and inside have the "damnit.setImageBitmap(bitt);"

Upvotes: 0

Views: 228

Answers (5)

Tom Sabel
Tom Sabel

Reputation: 4025

You are trying to get an url from the MainThread. That is not possible. This is the error you will get: android.os.NetworkOnMainThreadException.

Try to use an AsyncTask for example to retrieve an Image from an url.

@Override
public void onCreate() {
    super.onCreate();

    ImageView damnit = (ImageView)findViewById(R.id.imageview);
    DownloadTask downloadTask = new DownloadTask(damnit);
    downloadTask.execute();
}

class DownloadTask extends AsyncTask <Void, Void, Bitmap>{
    private ImageView imageView;

    public DownloadTask(ImageView imageView) {
        this.imageView = imageView;
    }
    @Override
    protected Bitmap doInBackground(Void... params) {
        try {
            URL url = new URL("https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png");
            InputStream in = url.openStream();
            return BitmapFactory.decodeStream(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
        if (bitmap != null) {
            imageView.setImageBitmap(bitmap);
        }
    }
}

Or use a library, for example Glide:

How to setup Glide:

build.gradle:

repositories {
  mavenCentral() // jcenter() works as well because it pulls from Maven Central
}

dependencies {
  compile 'com.github.bumptech.glide:glide:3.7.0'
  compile 'com.android.support:support-v4:19.1.0'
}

onCreate:

Glide.with(this)
    .load("https://www.some-url.com/image.jpg")
    .into(imageView);

Upvotes: 2

sonngaytho
sonngaytho

Reputation: 111

I think you should use Universal Image Loader. It is great library. Visit: https://github.com/nostra13/Android-Universal-Image-Loader

Upvotes: 0

Venkatesh Selvam
Venkatesh Selvam

Reputation: 1412

Use the Glide Library to load the images & set into ImageView:

Why i should recommend the Glide Library?

Because its very fast & consumes less memory.

Glide is a fast and efficient open source media management and image loading framework for Android that wraps media decoding, memory and disk caching, and resource pooling into a simple and easy to use interface.

How to do you Set up Glide:

Different ways to setup the Glide library to your project

1.You can download a jar from GitHub's releases page.

Or

2.use Gradle --> build.gradle(Module:app)

repositories {
  mavenCentral() // jcenter() works as well because it pulls from Maven Central
}

dependencies {
  compile 'com.github.bumptech.glide:glide:3.7.0'
  compile 'com.android.support:support-v4:19.1.0'
}

Or

3.Use Maven:

<dependency>
  <groupId>com.github.bumptech.glide</groupId>
  <artifactId>glide</artifactId>
  <version>3.7.0</version>
</dependency>
<dependency>
  <groupId>com.google.android</groupId>
  <artifactId>support-v4</artifactId>
  <version>r7</version>
</dependency>

How do you use Glide?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView damnit = (ImageView)findViewById(R.id.imageview);

    Glide.with(this).load("https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png").into(damnit);

    }

Happy Coding :)

Upvotes: 1

Devender Kumar
Devender Kumar

Reputation: 89

You can use UrlImageViewHelper library. get it here- http://repository.sonatype.org/service/local/artifact/maven/redirect?r=central-proxy&g=com.koushikdutta.urlimageviewhelper&a=urlimageviewhelper&v=LATEST

and build a method and use it wherever you want-

public void setPicture(Context context, String url, ImageView imageView, int placeholder) {
    UrlImageViewHelper.setUrlDrawable(imageView, url, placeholder);
}

Upvotes: 0

Ahmed M. Abed
Ahmed M. Abed

Reputation: 609

You can do this easily by using image library. Try Picasso it's an open source library for downloading and caching images from web, it's also easy to use.

Just add this line in dependancies section in gradle file:

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

Then, start to use it like this:

Picasso.with(context)
    .load(url)
    .placeholder(R.drawable.user_placeholder)
    .error(R.drawable.user_placeholder_error)
    .into(imageView);

Important note : avoid to pass empty url to it because it will be throw an exception.

Upvotes: 0

Related Questions