我正在制作教育用途,一款Android APP。 这个APP,调用服务(服务在任务完成时死亡)并拍摄没有UI的照片,没有声音。
这个APP在我的旧HTC Desire(root)中运行良好,但不能在Nexus4中启动。我做错了什么?为什么在certanly手机上工作但在其他手机上没有?
代码:
public class OIM_CameraControl extends Service
{
private final IBinder mBinder = new OIM_CameraControlBinder();
private Camera mCamera;
private Parameters parameters;
private TelephonyManager telephonyManager;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
SurfaceView sv = new SurfaceView(getApplicationContext());
if(mCamera == null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
Camera.CameraInfo info=new Camera.CameraInfo();
for (int i=0; i < Camera.getNumberOfCameras(); i++) {
Camera.getCameraInfo(i, info);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
mCamera=Camera.open(i);
}
}
}
}
if (mCamera == null) {
System.out.println("No se ha detectado ninguna camara");
mCamera=Camera.open();
}
mCamera.setPreviewDisplay(sv.getHolder());
// I tried change parameters, add new parameters, delete parameters
// a lot of combinations,... never works in Nexus4.
parameters = mCamera.getParameters();
parameters.setPreviewSize(720, 480);
parameters.setPictureSize(720, 480);
parameters.setFocusMode(Parameters.FOCUS_MODE_FIXED);
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
parameters.setZoom(0);
parameters.set("orientation", "portrait");
parameters.set("rotation",90);
mCamera.setParameters(parameters);
mCamera.startPreview();
mCamera.takePicture(null, null, mCall);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
return START_NOT_STICKY;
}
private Camera.PictureCallback mCall = new Camera.PictureCallback()
{
public void onPictureTaken(byte[] data, Camera camera)
{
//Do something
}
};
//CLASES INTERNAS DE BINDER
public class OIM_CameraControlBinder extends Binder {
public OIM_CameraControl getService() {
return OIM_CameraControl.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
在我的清单中:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera.flash" />
logcat的:
06-17 11:25:29.620: D/Camera(14346): app passed NULL surface
06-17 11:25:29.650: W/System.err(14346): java.lang.RuntimeException: takePicture failed
06-17 11:25:29.660: W/System.err(14346): at android.hardware.Camera.native_takePicture(Native Method)
06-17 11:25:29.660: W/System.err(14346): at android.hardware.Camera.takePicture(Camera.java:1095)
06-17 11:25:29.660: W/System.err(14346): at android.hardware.Camera.takePicture(Camera.java:1040)
06-17 11:25:29.660: W/System.err(14346): at com.obviousdevelop.estigia.objects.implement.OIM_CameraControl.onStartCommand(OIM_CameraControl.java:57)
06-17 11:25:29.660: W/System.err(14346): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2656)
06-17 11:25:29.660: W/System.err(14346): at android.app.ActivityThread.access$1900(ActivityThread.java:141)
06-17 11:25:29.660: W/System.err(14346): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
06-17 11:25:29.660: W/System.err(14346): at android.os.Handler.dispatchMessage(Handler.java:99)
06-17 11:25:29.660: W/System.err(14346): at android.os.Looper.loop(Looper.java:137)
06-17 11:25:29.660: W/System.err(14346): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-17 11:25:29.660: W/System.err(14346): at java.lang.reflect.Method.invokeNative(Native Method)
06-17 11:25:29.660: W/System.err(14346): at java.lang.reflect.Method.invoke(Method.java:511)
06-17 11:25:29.660: W/System.err(14346): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-17 11:25:29.660: W/System.err(14346): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-17 11:25:29.660: W/System.err(14346): at dalvik.system.NativeStart.main(Native Method)
提前致谢!
答案 0 :(得分:0)
检查,拍照后是否再次致电Camera.startPreview()
。
请点击链接并检查您的代码并与之匹配..
http://developer.android.com/reference/android/hardware/Camera.html