How to get relative rotation matrix from two orientation values in android?
Your error might be due to the indices used. Nevertheless, it is always better to state the proper procedure to do it, so that you could find your error.

Let us assume that a 3D scene point $P$ is related to the normalized coordinates of the camera $i$ with the extrinsic parameters $P'_i=R_iP+T_i$ and similarly to camera $j$ with $P'_j=R_jP+T_j$. To find the relative pose we could simply use these two equations to go from the camera coordinates of view $i$ to the real scene and then to the second view $j$. We could then write:

$P=R_i^{-1}(P_i-T_i)$ (from camera $i$ to world)

$P'_j = R_j (R_i^{-1}(P_i-T_i)) +T_j$.  (from world to camera $j$)

If we re-arrange:

$P'_j = R_{rel}P_i +T_{rel}$, where $R_{rel}=R_jR_i^{-1}$ and $T_{rel}=-R_{rel}T_i+T_j$.

where the subscript rel refers to "relative" and is what we seek to find. Keep in mind that, as rotations are orthogonal, $R_i^{-1}=R_i^T$. You can always compute relative orientations in similar manners.

Throughout the computation we used the convention that the transformation $J=[R|T]$ is a mapping from scene to the camera (the inverse is hence the vice versa). The final pose transforms a point in camera $i$ to camera $j$.