Quickest Approach:
You can try either of these image downloading library for Android - Picasso or Glide. You can use either one in your Fragment or your Activity or your Adapter:
Picasso:
// .with() only takes Context objects
Picasso.with(context)
.load("http://image10.bizrate-images.com/resizesq=60&uid=2216744464")
.into(imageView);
Glide:
// .with() can take Context, Activity, Fragment or FragmentActivity objects.
Glide.with(context)
.load("http://image10.bizrate-images.com/resizesq=60&uid=2216744464")
.into(imageView);
Here's a link that explains the similarities and differences between both libraries.
Another Approach:
It involves creating a Service to fetch the data (on a background thread), saving the url into a bitmap and finally sending that bitmap back to the UI thread to be saved in the ImageView.
Steps:
- Create a Service (and add to the manifest).
- Service should extend IntentService (and implement
onHandleIntent
).
- Retrieve url from intent.
- Fetch image from url.
- Decode and save the url to a Bitmap.
- Put the Bitmap inside an EventBus to send to the UI thread.
- Set Bitmap to ImageView.
Manifest:
<application
...
<activity android:name=".MainActivity">
...
</activity>
<service android:name=".ImageFetchService" />
</application>
Eventbus dependency (gradle for Android Studio, Naxam-EventBus.Droid for Visual Studio):
dependencies {
//
compile 'org.greenrobot:eventbus:3.0.0'
}
Install-Package Naxam.EventBus.Droid // Use this instead since Xamarin.Android doesn't have gradle.
Service Class:
public class ImageFetchService extends IntentService {
public ImageFetchService() {
super("ImageFetchService");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
String urlString = intent.getData().toString();
URL url;
try {
url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
BitmapEvent bitmapEvent = new BitmapEvent();
bitmapEvent.setBitmap(myBitmap);
EventBus.getDefault().post(bitmapEvent);
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
}
Now, we need a class that will encapsulate the bitmap object sent from our Service class. EventBus transmits objects also known as events
and those events can contain any number of other objects inside it - think of an EventBus as a bus that transports a POJO (or event) from one place to another.
In this case, the EventBus will transport our Bitmap from the background thread to the UI thread.
BitmapEvent Class (our POJO for EventBus):
public class BitmapEvent {
private Bitmap bitmap;
public BitmapEvent() {
//
}
public Bitmap getBitmap() {
return bitmap;
}
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.image);
String urlString = "http://image10.bizrate-images.com/resizesq=60&uid=2216744464";
Intent fetchImageIntent = new Intent(this, ImageFetchService.class);
fetchImageIntent.setData(Uri.parse(urlString));
startService(fetchImageIntent);
}
@Override
protected void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
protected void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void getBitmapEvent(BitmapEvent bitmapEvent) {
imageView.setImageBitmap(bitmapEvent.getBitmap());
}
}
Note: Depending on your internet connection and the size of the image, you may notice some delay before getting the ImageView to update with the bitmap.
You can look into AsyncTask as an alternative to EventBus.