0

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);"

1
  • 1
    this is a nice exercise, but you really should use a 3rd party library to do this... Commented Apr 13, 2016 at 12:50

5 Answers 5

2

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);
Sign up to request clarification or add additional context in comments.

5 Comments

nope, still does not work in a new thread, or async task. also i hate using libraries.
Did you retrieve the bitmap in doInBackground and set the image in onPostExecute? See updated answer. Note. I don't judge you by not wanting libraries. In my young days of developing I didn't like it either. After a while I noticed that libraries can really be useful when project are large.
Well this is not really big at all, and tbh that code there does not work at all. i have no idea what just happened. it was working perfectly before, but i dont know what i did :(
Did you add the doInBackground and onPostExecute in an (inner) AsyncTask class?
yes, ugh. maybe i should just read the image into a byte array and then decode it and maybe it would work?
1

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 :)

Comments

0

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.

Comments

0

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);
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.