Saturday, 31 August 2013

Android taking pictures asynchronously inside a broadcast receiver

Android taking pictures asynchronously inside a broadcast receiver

I have been struggling with the above for a while now, so would really
appreciate any answers.
Firstly, my main aim is to take a picture when I receive an intent (
inside a broadcast receiver). I have tried quite a few approaches to the
above but to no avail. The following (showing relevant code snippets for
visual clarity) code illustrates one approach:
public class ArduinoReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
takePictureUsingCamera();
} }
The function takePictureUsingCamera() calls a function in a class that
deals with the camera ( e.g displaying a camera preview in a FrameLayout).
The particular function calls the android takePicture function in android:
this.mPreview.mCamera.takePicture(null, null, this.mPicture);
However when I receive the intent, the app crashes. I felt that the reason
behind this was that taking a picture was perhaps asking for too much work
to be done inside the broadcast receiver. (I tested this functionality
using a simply picture capture using a button and it worked perfectly. )
So, the second approach that I tried was to create a Intentservice which
will deal with the taking of pictures ( and other relevant tasks like
storing pictures), when an intent is received.
The Service's onHandleIntent(Intent intent) method will now have the
this.mCameraView.takePicture(filename) function. However, I need to pass
my CameraView class as an object to this service using the intent for
which I created a Serializable class and tried passing this using the
intent's putExtra method. After a few crashes, I realized that the
CameraView Class was not serializable (it is displaying the camera preview
in a FrameLayout).
Another alternative way I thought of doing the above was to create the
cameraView object itself inside the new service but for this I need the
context and the reference to the main activity - which I don't think can
be passed using intents. This leaves the option of starting a new activity
to take the picture inside the broadcast receiver - again looks like doing
too much work. As I said before, I've tried a few approaches but haven't
achieved success in any of them. I personally didn't think its going to be
such a problem. I would really appreciate and be quite thankful to any
answerers.

No comments:

Post a Comment