Saturday, November 20, 2010

Using the camera (from your programs) on Android is easy ...

... if you know how to do it.

If you don't know it, it can be a PITA as I have e.g. described at the Android-Tech-Talk in Stuttgart.

For Zwitscher I was for quite some time trying to enable it to take pictures and upload them to pictures services like with other Twitter clients. So I started looking at the documentation and found a How To entry which basically points to the documentation for the Camera.  So I've been trying around with the preview stuff and was googling like crazy and so on, but this turned out to be too complicated for me to follow in the short term.

Yesterday I was reading about Intents in the Documentation and what Google Intents are available. This brought me to the idea of investigating if the camera could also be called that way.

So I've googled for "android camera app intent" and found this forum post which explains how to do it.

Basically start the camera via:

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);

and then later when the picture has been taken fetch the picture in the onActivityResult() callback:

public void onActivityResult(int requestCode, int resultCode,Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1&& resultCode==RESULT_OK) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");

This takes a small picture suitable for e.g. Twitter.

To take a larger picture you need to tell the intent where to store a larger picture and pick it up from that location.

intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, path)

So you see - if you know all this, it is fairly easy to start the camera, take a picture and then use it later. No dealing with PreviewHolder and all that stuff.


No comments: