Camera - onTakePicture wird nicht ausgelöst

  • Antworten:0
Ma MA
  • Forum-Beiträge: 1

25.06.2014, 17:51:12 via Website

Hallo Zusammen,

Ich entwickle eine Android App die bei einer Nachricht von Google Cloud Messaging einen Screenshot mit der Front Kamera machen soll.
Bisher habe ich erreicht, dass die Nachrichten sauber übermittelt und durch einen Toast angezeigt werden. Beim Photo mit der Kamera habe ich momentan noch einen (Laufzeit-)Fehler auf den ich leider nicht komme:

Alle Funktionen im Quellcode werden ohne Fehlermeldung ausgeführt. Die Funktion "OnPictureTaken" im PhotoHandler wird allerdings nicht aufgerufen ... Wenn ich einen Quelltexthaltepunkte ab dem "OnMessage" einfüge, funktioniert das Photo allerdings und das Bild wird gespeichert ...

Bitte helft mir...

@Override
protected void onMessage(final Context context, Intent msg) 
{
    // Handle the message.
     final String message = msg.getStringExtra("message");

     Log.d(konstanten.DEBUG_PHOTO, "onMessage called:" + message);

     Handler h = new Handler(Looper.getMainLooper());
     h.post(new Runnable()
     {

          public void run() 
          {
              // TODO Auto-generated method stub
              Log.d(konstanten.DEBUG_PHOTO, "Handler started");
              Toast.makeText(context, message, Toast.LENGTH_LONG).show();
              TakePicture();
          }         
      });        
}

Camera camera;
int cameraId = 0;

public void TakePicture()
{


    try 
    {
        // do we have a camera?
        if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) 
        {
            Log.d(konstanten.DEBUG_PHOTO, "No Camera");
            Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG).show();
        } 
        else 
        {
            cameraId = findFrontFacingCamera();
            if (cameraId < 0) 
            {
                Log.d(konstanten.DEBUG_PHOTO, "No front camera found!");
                Toast.makeText(this, "No front facing camera found.", Toast.LENGTH_LONG).show();
            } 
            else 
            {
            try
                {

                    if (camera != null)
                    {
                        Log.d(konstanten.DEBUG_PHOTO, "camera release");
                        camera.release();
                    }

                    camera = Camera.open(cameraId);    
                    camera.startPreview(); 
                    Log.d(konstanten.DEBUG_PHOTO, "Take Picture ...");
                    camera.takePicture(null, null, new PhotoHandler(this));
                    Log.d(konstanten.DEBUG_PHOTO, "Picture successfully taken ..");
                    Toast.makeText(getApplicationContext(), "camera takepicture", Toast.LENGTH_SHORT).show();
                    }
                    catch (Exception e)
                    {
                        Log.d(konstanten.DEBUG_PHOTO, "ERROR: " + e.getMessage());
                        Toast.makeText(getApplicationContext(), "ERROR: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                    } 
                }

            }

    } catch (Exception e)
    {
        Log.d(konstanten.DEBUG_PHOTO, "Error: " + e.getMessage());
        Toast.makeText(getApplicationContext(), "ERROR:" + e.getMessage(), Toast.LENGTH_SHORT).show();
    }

}

private int findFrontFacingCamera() 
{
    int cameraId = -1;
    // Search for the front facing camera
    int numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numberOfCameras; i++) 
    {
        CameraInfo info = new CameraInfo();
        Camera.getCameraInfo(i, info);
        if (info.facing == CameraInfo.CAMERA_FACING_FRONT) 
        {
            Log.d(konstanten.DEBUG_PHOTO, "Camera found");
            cameraId = i;
            break;
        }
    }
    return cameraId;
}

Photo Handler:

package com.example.messenger;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

public class PhotoHandler implements PictureCallback 
{

    private final Context context;

    public PhotoHandler(Context context) 
    {
        Log.d(konstanten.DEBUG_PHOTO, "PhotoHandler constructor called");
        this.context = context;
    }

    @Override
    public void onPictureTaken(byte[] data, Camera camera) 
    {

        Log.d(konstanten.DEBUG_PHOTO, "onPictureTaken called");
        Toast.makeText(context, "OnPictureTaken", Toast.LENGTH_LONG).show();
        File pictureFileDir = getDir();

        if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) 
        {
            Log.d(konstanten.DEBUG_PHOTO, "Can't create directory to save image.");
            Toast.makeText(context, "Can't create directory to save image.", Toast.LENGTH_LONG).show();
            return;
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
        String date = dateFormat.format(new Date());
        String photoFile = "Picture_" + date + ".jpg";

        String filename = pictureFileDir.getPath() + File.separator + photoFile;

        File pictureFile = new File(filename);

        try 
        {
            Log.d(konstanten.DEBUG_PHOTO, "Begin Fileoutputstream");
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
            Toast.makeText(context, "New Image saved:" + photoFile,
                    Toast.LENGTH_LONG).show();
            Log.d(konstanten.DEBUG_PHOTO, "Image saved: " + photoFile);
        } catch (Exception error) {
            Log.d(konstanten.DEBUG_PHOTO, "File" + filename + "not saved: "
                    + error.getMessage());
            Toast.makeText(context, "Image could not be saved.",
                    Toast.LENGTH_LONG).show();
        }
        camera.stopPreview();
        camera.release();
        Log.d(konstanten.DEBUG_PHOTO, "onTakePicture end");
    }

    private File getDir() 
    {
        File sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        return new File(sdDir, konstanten.AppName);
    }
}

Antworten