The camera is released when the activity is paused (assuming that your code is based on CameraEx example).
Thanks Erel,
Yes it is based on CameraEx example. But in this example too, I understand the camera is initialized back in activity resume isn't it ?
I modified a bit the activity resume routine in order to wait for the camera ready status and take the image and I get another runtime exception :
PackageAdded: package:test.power
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
90
[Height=720, IsInitialized=false, Width=960
]
camera ready
** Service (test_service) Create **
** Service (test_service) Start **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (CallSubDelayed - activity_resume)
running waiting messages (1)
90
[Height=720, IsInitialized=false, Width=960
]
camera ready
** Activity (main) Resume **
cameraexclass_takepicture (B4A line: 102)
cam.TakePicture
java.lang.RuntimeException: takePicture failed
at android.hardware.Camera.native_takePicture(Native Method)
at android.hardware.Camera.takePicture(Camera.java:1103)
at android.hardware.Camera.takePicture(Camera.java:1048)
at anywheresoftware.b4a.objects.CameraW.TakePicture(CameraW.java:287)
at test.power.cameraexclass._takepicture(cameraexclass.java:1130)
at test.power.main._takepicture(main.java:682)
at test.power.main._activity_resume(main.java:392)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:154)
at test.power.main$ResumeMessage.run(main.java:254)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)
The occurence of this error is when I turn off the screen. The services sends a callsubdelayed main activity resume which is supposed to take the picture:
Here is the service piece of code:
Sub PE_ScreenOff (Intent As Intent)
'If CheckLock Then ' si le tel est verouillé ...
' If DateTime.Now - mydate < 2000 Then
Vibrate.Vibrate(200)
' <============ add the camera take picture here
CallSubDelayed(Main,"activity_resume")
'CallSubDelayed(Main,"InitializeCamera")
'CallSubDelayed(Main , "TakePicture")
' End If
'End If
' Log ("screen off")
mydate = DateTime.Now
End Sub
Here is the main code:
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("1")
PE.Initialize("PE")
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim p As PhoneSms
Dim PE As PhoneEvents
Dim phonepower As String
Dim Button1 As Button
Dim label1 As Label
Dim Panel1 As Panel
Private camEx As CameraExClass
End Sub
Sub Process_Globals
Dim Timer1 As Timer
Timer1.Initialize("Timer1",1000)
Private frontCamera As Boolean = False
Dim camera_prete As Boolean = False
End Sub
Sub Activity_Resume
'Activity.LoadLayout("1")
InitializeCamera
'ToastMessageShow(camera_prete, False)
Do While camera_prete = False
'Log(camera_prete)
DoEvents
Loop
ToastMessageShow(camera_prete, False)
TakePicture
End Sub
Sub Button1_Click
Timer1.Enabled=True
StartService(test_service)
' p.Send("+33688941445","helloworld : " & DateTime.Now)
End Sub
Sub Timer1_Tick
label1.Text=test_service.Counter
End Sub
Sub Activity_KeyPress (keycode As Int) As Boolean
'Return True to consume the event
Msgbox(keycode, "")
Return True
End Sub
Sub PE_BatteryChanged(Level As Int, Scale As Int, Plugged As Boolean, Intent As Intent)
If Plugged Then
phonepower = " sur secteur"
Else
phonepower = " pas de secteur"
End If
Msgbox(phonepower,"")
' p.Send("+33688941445","helloworld : " & phonepower)
End Sub
Private Sub InitializeCamera
camEx.Initialize(Panel1, frontCamera, Me, "Camera1")
frontCamera = camEx.Front
End Sub
Sub Activity_Pause (UserClosed As Boolean)
camEx.Release
camera_prete = False
End Sub
Sub Camera1_Ready (Success As Boolean)
If Success Then
camEx.SetJpegQuality(90)
camEx.CommitParameters
camEx.StartPreview
Log(camEx.GetPreviewSize)
Log("camera ready")
camera_prete = True
Else
Log("camera not ready")
ToastMessageShow("Cannot open camera.", True)
End If
End Sub
Sub TakePicture
camEx.TakePicture
End Sub
Sub Camera1_PictureTaken (Data() As Byte)
Dim filename As String = "1.jpg"
Dim dir As String = File.DirRootExternal
camEx.SavePictureToFile(Data, dir, filename)
camEx.StartPreview 'restart preview
'send a broadcast intent to the media scanner to force it to scan the saved file.
Dim Phone As Phone
Dim i As Intent
i.Initialize("android.intent.action.MEDIA_SCANNER_SCAN_FILE", _
"file://" & File.Combine(dir, filename))
Phone.SendBroadcastIntent(i)
ToastMessageShow("Picture saved." & CRLF & "File size: " & File.Size(dir, filename), True)
End Sub
Sub Delay(nMilliSecond As Long)
Dim nBeginTime As Long
Dim nEndTime As Long
nEndTime = DateTime.Now + nMilliSecond
nBeginTime = DateTime.Now
Do While nBeginTime < nEndTime
nBeginTime = DateTime.Now
Log(nBeginTime)
If nEndTime < nBeginTime Then
Return
End If
DoEvents
Loop
End Sub
It seems the problem is coming from the fact the panel1 is not displayed on the screen ?
I wonder how I can put the main activity back on screen from the service ?
Alain