Pick image using Rx-Android

In this tutorial we are not going to the introductory part or a deep drive into the Rx. There are lot more tutorials for these on the web. Here we will see a simple and just to understand the usage (I said just to understand) of Rx in android. We just thought to share this easy to understand example.

So the scenario is while picking the image from gallery / capturing through camera from various places like from dialog, dialog fragment, etc. we will pass the event to the bus. As these picked images are passed to onActivityResult and then can be passed to fragment and then to dialog and go on. So we just wanted to remove the usage of onActivityResult from fragment and just wanted to communicate the result directly to the point of starting selection. Definitely we can use builder pattern, but as said above just to understand.


What we are going to do?

  • Implement eventbus using Rx-Android
  • Image capture/pick from fragment
  • Image capture/pick from dialog fragment
  • Image capture/pick from dialog
  • These three components will be nested
  • No use of onActivityResult other than MainActivity.

We used to implement the event bus which you might have implemented through Otto or Greenrobot eventbus. We will implement eventbus here using rx-android.

Eventbus here will act as a mediator between source of event and destination where we want to pass this event.

So let us start with including gradle dependencies in our project.

1> Open your build.gradle (Module:app) & include following dependency:

After adding dependency just rebuild your project, it will download the required files.

 

2> Now create a simple Pojo.java class to store & pass the event data at required location.

 

3> Now implement our singleton Rx eventbus.java.

Here Subject class in rx-java has properties of both, observable and observer, as it extends observable and implements observer. So a subject can subscribe to Observables and can receive data. It can emit data to subscribers. So it works as a mediator here.

Subject<Object, Object> subscriberSubjects is actually Subject<T, R>.
Where T is the type of data a subject will emit & R is the type of data it will receive from observables. There are other subjects as well with their specialized use case.

Here we have used basic specialized subject. It emits data to its subscribers whenever its onNext method is called.

 

4> Create an interface OnOperationResult.java.

This interface will be used to return result to our starting point.

 

5> Next is our MyImagePicker.java class.

This class doesn’t need more explanation (pretty straight forward). It is important to note that we should subscribe to the same bus which was used to post the event. The class shows how to subscribe and get the latest event.

Here, Action is nothing but a part of subscriber. Actually subscribe() has six overloaded versions, look at the source code. Action here defines onNext and others can be onError and onCompleted, which are called by observables – on emission, error and completion respectively. Whatever, we are interested in first one only right now.

 

So now this block of code is enough to get image selection from any part of your app.

 

6> Now let us make a MyDialogFragment.java class.

 

7>  Now add some code to MainActivity.java.

Don’t forget to add code inside onActivityResult. It adds the image selection result to our Rx bus.

activity_main.xml

 

7>  MyConstants.java contains all used constants in above classes.

 

8>  Below is image_selection.xml – used for fragment, dialogfragment and dialog.

 

9>  Don’t forget to add permission to manifest.

To manage Run time permissions for devices with later apis please visit here.

Now run the project and enjoy 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *