开发者

Loading remote images

开发者 https://www.devze.com 2023-01-03 20:43 出处:网络
In Android, what is the simplest approach to the following: Load an image from a remote server. Di开发者_JS百科splay it in an ImageView.

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

  1. Load an image from a remote server.
  2. Di开发者_JS百科splay it in an ImageView.


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.


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.


Be careful with both of the answers here - they both run the chance of an OutOfMemoryException. Test your application by attempting to download a large image, such as a desktop wallpaper. To be clear, the offending lines are :

final Bitmap bm = BitmapFactory.decodeStream(bis);

and

Drawable thumb_d = Drawable.createFromStream(thumb_u.openStream(), "src");

Felix's answer will catch it in the catch{} statement, and you could do something about it there.

Here is how to work around the OutOfMemoryException error:

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 8;
    Bitmap bmp = null;
    try {
        bmp = BitmapFactory.decodeStream(is, null, options);
    } catch (OutOfMemoryError ome) {
        // TODO - return default image or put this in a loop,
        // and continue increasing the inSampleSize until we don't
        // run out of memory
    }

And here are my comments on this in my code

/**
 * Showing a full-resolution preview is a fast-track to an
 * OutOfMemoryException. Therefore, we downsample the preview image. Android
 * docs recommend using a power of 2 to downsample
 * 
 * @see <a
 *      href="https://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966">StackOverflow
 *      post discussing OutOfMemoryException</a>
 * @see <a
 *      href="http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize">Android
 *      docs explaining BitmapFactory.Options#inSampleSize</a>
 * 
 */

Links from above comments: Link 1 Link 2


You can also try this lib: https://github.com/codingfingers/fastimage

When we had few projects with the same pattern, and the lib came up ;) so why not to share with others...


This is simple:

Add this dependency in you gradle script:

implementation 'com.squareup.picasso:picasso:2.71828'

*2.71828 is current version

Then do this for the image view:

Picasso.get().load(pictureURL).into(imageView);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号