Android: how to set image to image using url programmatically

I have an image url coming from my leisure API. Now I want to set it to an image when activity is loading. Below is how I get a bean from the rest of the api and then get the url from it.

Message message=new Message();
String imageUrl=message.getImageUrl();

I get a Message object from my database, and the image URL is included in this message object.

Then I used the Url object to get this image url.

URL url = null;
try {
     url = new URL(imageUrl);
     Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
     contentImageView.setImageBitmap(bmp);
} catch (Exception e) {
     e.printStackTrace();
}

I used the above codes to load an image into an image object, which is contentImageView.

But still I can’t upload this image to imageview, nothing is loading.

any ideas?

+4
4

- - Picasso Glide:

Picasso.with(getContext()).load(imgUrl).fit().into(contentImageView);

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

+9

, , bitmap

public Bitmap getBitmapfromUrl(String imageUrl)
{
    try
    {
        URL url = new URL(imageUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap bitmap = BitmapFactory.decodeStream(input);
        return bitmap;

    } catch (Exception e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;

    }
}
+3

Glide picasa

Dependices

compile 'com.github.bumptech.glide:glide:3.7.0'

  Glide.with(this)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
                .into(imageview);

:   Glide https://github.com/bumptech/glide

+2

- :

  • setImageBitmap (Bitmap bm)// ImageView.

  • setImageResource (int resId) // Sets drawable as the content of this ImageView.

Link: https://developer.android.com/reference/android/widget/ImageView.html

+1
source

Source: https://habr.com/ru/post/1677136/


All Articles