Reputation: 35235
In Android, what is the simplest approach to the following:
Upvotes: 11
Views: 25549
Reputation: 2261
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);
Upvotes: 4
Reputation: 89566
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.
Upvotes: 23
Reputation: 525
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...
Upvotes: 6
Reputation: 21552
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
Upvotes: 6
Reputation: 200080
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.
Upvotes: 6