我开始使用Google Cloud Messaging编写Android应用程序。当收到来自GCM的消息时,我想用前置摄像头拍摄照片。 我可以发送信息,接收信息并开始拍照。一切看起来都很好,但onPictureTaken()从未调用过每次...... 只有当我设置一个断点时,它才能正常工作并制作图片并保存,当没有设置断点或者我没有使用调试器时,它不起作用..
它看起来像运行时问题..
我查看了解决方案,但我没有找到任何有用的信息..
请帮助我!
编辑: 当我在“活动创建”中执行“cam = Camera.open(CamID)”时,它会在意图服务中成功运行第一张照片...当我调用cam.takepicture()时,它看起来没有正确初始化。 有人有想法吗?
@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;
}
这是我的PhotoHandler课程:
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);
}
}
LogCat-Output(对于DEBUG_PHOTO-Tag):
06-25 16:41:37.831: D/PHOTO-HANDLER(16917): onMessage called:Hello World!
06-25 16:41:37.841: D/PHOTO-HANDLER(16917): Handler started
06-25 16:41:37.876: D/PHOTO-HANDLER(16917): Camera found
06-25 16:41:38.886: D/PHOTO-HANDLER(16917): Take Picture ...
06-25 16:41:38.911: D/PHOTO-HANDLER(16917): PhotoHandler constructor called
06-25 16:41:39.021: D/PHOTO-HANDLER(16917): Picture successfully taken ..