Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading remote images

In Android, what is the simplest approach to the following:

  1. Load an image from a remote server.
  2. Display it in an ImageView.
like image 980
Emanuil Rusev Avatar asked Jun 19 '10 13:06

Emanuil Rusev


People also ask

What is loading remote images?

Remote images are part of an email that is not included in the message itself but is downloaded from the internet when you view the email.

What does load remote content in messages mean?

Remote content are parts of a message (such as images, stylesheets, or videos) which are not included in the message itself, but are downloaded from the Internet when you view the message. Remote content is a privacy concern because it allows the message sender to know: each time you view the message.

Why does my iPhone say unable to load remote content privately?

Message: "Unable to load remote content privately" appears at the top of an email on your iOS device. This message appears in iOS 15.0 or later versions if Apple's Mail Privacy Protection feature is enabled on your device. In some cases, this message may also appear if you are using a VPN for your device.


2 Answers

Here's a method that I actually used in an application and I know it works:

try {
    URL thumb_u = new URL("http://www.example.com/image.jpg");
    Drawable thumb_d = Drawable.createFromStream(thumb_u.openStream(), "src");
    myImageView.setImageDrawable(thumb_d);
}
catch (Exception e) {
    // handle it
}

I have no idea what the second parameter to Drawable.createFromStream is, but passing "src" seems to work. If anyone knows, please shed some light, as the docs don't really say anything about it.

like image 178
Felix Avatar answered Nov 22 '22 10:11

Felix


The easiest way so far is build a simple image retriver:

public Bitmap getRemoteImage(final URL aURL) {
    try {
        final URLConnection conn = aURL.openConnection();
        conn.connect();
        final BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
        final Bitmap bm = BitmapFactory.decodeStream(bis);
        bis.close();
        return bm;
    } catch (IOException e) {}
    return null;
}

Then, you just have to supply a URL to the method and it will returns a Bitmap. Then, you will just have to use the setImageBitmap method from ImageView to show the image.

like image 20
Cristian Avatar answered Nov 22 '22 12:11

Cristian