Android Question java.lang.RuntimeException: takePicture failed with CameraExClass 1.30

josejad

Expert
Licensed User
Longtime User
Hi all:

My workmates use my app to make a lot of pics of their jobs. I use to make the pictures CameraExClass 1.30.
Some of them get an error while making pictures, and I can't reproduce it. I mean, they say they're just pressing the take picture button, and sometimes they get this error.
I get an error in checklist_activity_create, but I think this is not the error that stops my app.
Probably the error stops my app is: "java.lang.RuntimeException: takePicture failed"

This is the code in the Cam1 activity, in which I take the picture
B4X:
Sub btnTakePicture_Click
    ProgressBar1.Visible = True
    ProgressBar1.BringToFront
    camEx.TakePicture
End Sub

Sub Camera1_PictureTaken (Data() As Byte)
    Dim NombreFoto As String
    Dim ListaFotos As List
    ListaFotos = Starter.SiteActual.MapFotos.GetVAlueAt(Spinner1.SelectedIndex)
    Do While File.Exists(Starter.Directorio & "/" & Starter.SiteActual.Site & "_" & Starter.SiteActual.Tecnologia &"/", Spinner1.SelectedItem.SubString2(0,4).Trim & "(" & IndiceFoto & ").jpg")
        IndiceFoto = IndiceFoto + 1
    Loop
    NombreFoto = Spinner1.SelectedItem.SubString2(0,4).Trim & "(" & IndiceFoto & ").jpg"
    Dim dir As String =Starter.Directorio & "/" & Starter.SiteActual.Site & "_" & Starter.SiteActual.Tecnologia &"/" 'Anteriormente  File.DirInternal
    camEx.SavePictureToFile(Data, dir, NombreFoto)
    camEx.StartPreview 'restart preview
    If File.Size(dir, NombreFoto) > 1500000 Then
        ToastMessageShow("Resolución demasiado alta, baje la resolución de la cámara a algún valor intermedio pulsando la rueda dentada", True)
    Else If File.Size(dir, NombreFoto) < 350000 Then
        ToastMessageShow("REPITA LA FOTO. Resolución demasiado baja, suba la resolución de la cámara a algún valor intermedio pulsando la rueda dentada.", True)
    End If
    ListaFotos.Add(NombreFoto)
    Starter.SiteActual.MapFotos.Put(Spinner1.SelectedItem, ListaFotos)
    ProgressBar1.Visible = False
End Sub

Thanks

They send me the error with logcat
B4X:
--------- beginning of system
--------- beginning of main
~i:*** Service (starter) Create ***
~i:** Service (starter) Start **
~i:** Activity (checklist) Create, isFirst = true **
~e:checklist_activity_create (java line: 380)
~e:java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
~e: at com.semi.bqa.checklist._activity_create(checklist.java:380)
~e: at java.lang.reflect.Method.invoke(Native Method)
~e: at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
~e: at com.semi.bqa.checklist.afterFirstLayout(checklist.java:104)
~e: at com.semi.bqa.checklist.access$000(checklist.java:17)
~e: at com.semi.bqa.checklist$WaitForLayout.run(checklist.java:82)
~e: at android.os.Handler.handleCallback(Handler.java:795)
~e: at android.os.Handler.dispatchMessage(Handler.java:99)
~e: at android.os.Looper.loop(Looper.java:166)
~e: at android.app.ActivityThread.main(ActivityThread.java:6861)
~e: at java.lang.reflect.Method.invoke(Native Method)
~e: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:450)
~e: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
java.lang.RuntimeException: takePicture failed
at android.hardware.Camera.native_takePicture(Native Method)
at android.hardware.Camera.takePicture(Camera.java:1513)
at android.hardware.Camera.takePicture(Camera.java:1455)
at anywheresoftware.b4a.objects.CameraW.TakePicture(CameraW.java:287)
at com.semi.bqa.cameraexclass._takepicture(cameraexclass.java:812)
at com.semi.bqa.cam1._btntakepicture_click(cam1.java:626)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6367)
at android.view.View$PerformClick.run(View.java:25040)
at android.os.Handler.handleCallback(Handler.java:795)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:6861)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:450)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
 

josejad

Expert
Licensed User
Longtime User
Sometimes. They can make a lot of pictures, then, the "lot+1" fails.
Re-open the app, and they can continue until the next error...
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
This is the code in the Cam1 activity, in which I take the picture
it is not related to the error you posted.

The error happens in activity_create of checklist activity.

Edit: i see the _btntakepicture_click in the log though.

Maybe better you upload a small project which shows the issue.
 
Last edited:
Upvote 0

josejad

Expert
Licensed User
Longtime User
Thanks DonManfred, I will check activity_create then again.
But... The camera is used in a different Activity ("Cam1"), so... I must be doing something in this activity that make activity_create of checklist activity to run?
I'm trying to discover which object is null and I'm trying to assign to an string. I will try to comment the Activity title, or maybe some of the variables in starter are destroyed?
'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

B4X:
'Checklist Activity
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("TabDescripciones")
    TabStrip1.LoadLayout("TabGenerales", Chr(0xF0CA) & " Generales " )
    TabStrip1.LoadLayout("TabEspecificas", Chr(0xF0CB) & " " & Starter.SiteActual.TipoInstalacion)
    Activity.AddMenuItem("Subir a FTP", "Upload")
    Activity.AddMenuItem("Ver/Ocultar Fotos", "Visualizar_Fotos")
    Activity.AddMenuItem("Leer Fotos", "Leer_Fotos")

    'Initialize Dialogs
    Base = Activity
    Dialog.Initialize (Base)
    ResumenSubida.Initialize 'As B4XLongTextTemplate
   
    Activity.Title = Starter.SiteActual.Site & " " & Starter.SiteActual.Tecnologia & " " & Starter.SiteActual.TipoInstalacion
   
    For Each lbl As Label In GetAllTabLabels(TabStrip1)
        lbl.Width = 50%x
        lbl.Typeface = Typeface.FONTAWESOME
    Next
    Starter.csvGeneral = Starter.csv.LoadCSV(File.DirAssets, "Generales.csv", ",")
    Starter.csvEspecifico = Starter.csv.LoadCSV(File.DirAssets, Starter.SiteActual.TipoInstalacion & ".csv",",")
    For Each Row() As String In Starter.csvGeneral
        Starter.MapGeneral.Put(Row(0), Row(1))
    Next
    For Each Row() As String In Starter.csvEspecifico
        Starter.MapEspecifico.Put(Row(0), Row(1))
    Next
End Sub
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Thank you Erel. I did it, but I was not sure if it would be usefull, and the line I saw doesn't give too much info:

Java:
public static String  _activity_create(boolean _firsttime) throws Exception{
anywheresoftware.b4a.objects.LabelWrapper _lbl = null;
String[] _row = null;
//BA.debugLineNum = 40;BA.debugLine="Sub Activity_Create(FirstTime As Boolean)";
//BA.debugLineNum = 54;BA.debugLine="Activity.LoadLayout(\"TabDescripciones\")";    
mostCurrent._activity.LoadLayout("TabDescripciones",mostCurrent.activityBA);            <--------------- LINE 380
//BA.debugLineNum = 55;BA.debugLine="TabStrip1.LoadLayout(\"TabGenerales\", Chr(0xF0CA)";
mostCurrent._tabstrip1.LoadLayout("TabGenerales",BA.ObjectToCharSequence(BA.ObjectToString(anywheresoftware.b4a.keywords.Common.Chr((int) (0xf0ca)))+" Generales "));
//BA.debugLineNum = 56;BA.debugLine="TabStrip1.LoadLayout(\"TabEspecificas\", Chr(0xF0CB";
mostCurrent._tabstrip1.LoadLayout("TabEspecificas",BA.ObjectToCharSequence(BA.ObjectToString(anywheresoftware.b4a.keywords.Common.Chr((int) (0xf0cb)))+" "+mostCurrent._starter._siteactual /*com.semi.bqa.main._tiposite*/ .TipoInstalacion /*String*/ ));
//BA.debugLineNum = 57;BA.debugLine="Log(\"Tipo Instalacion: \" & Starter.SiteActual.Tip";
anywheresoftware.b4a.keywords.Common.LogImpl("7458769","Tipo Instalacion: "+mostCurrent._starter._siteactual /*com.semi.bqa.main._tiposite*/ .TipoInstalacion /*String*/ ,0);
//BA.debugLineNum = 58;BA.debugLine="Activity.AddMenuItem(\"Subir a FTP\", \"Upload\")";
mostCurrent._activity.AddMenuItem(BA.ObjectToCharSequence("Subir a FTP"),"Upload");
//BA.debugLineNum = 59;BA.debugLine="Activity.AddMenuItem(\"Ver/Ocultar Fotos\", \"Visual";
mostCurrent._activity.AddMenuItem(BA.ObjectToCharSequence("Ver/Ocultar Fotos"),"Visualizar_Fotos");
//BA.debugLineNum = 60;BA.debugLine="Activity.AddMenuItem(\"Leer Fotos\", \"Leer_Fotos\")";
mostCurrent._activity.AddMenuItem(BA.ObjectToCharSequence("Leer Fotos"),"Leer_Fotos");
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
How and when is the checklist Activity started in normal conditions?
Is it the next part of the program flow after taking a correct pic? Also, is it launched automatically when a condition is met, or manually upon UI interaction?

Worth checking:
Make sure that the takepicture button (or the action that it throws) is disabled after being pressed until the operation has completed (either successfully or with error). Otherwise the user can press it twice by error, and throw some error event after a while that isn't being correctly managed because a new activity is being started.
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Hi Jordi:


How and when is the checklist Activity started in normal conditions?
That's the strange thing to me. From the "checklist" activity, I call the "Cam1" activity, and I didn't call "checklist" activity from "cam1", you take a picture and the app continue in "cam1" until you press back key.
That's why I thought the problem was really in the "java.lang.RuntimeException: takePicture failed". So, and now I see the last Erel's post, for sure worth to check what you say.

I've added:
B4X:
Sub btnTakePicture_Click
    ProgressBar1.Visible = True
    ProgressBar1.BringToFront
    btnTakePicture.Enabled = False                         '<------------- ADDED
    Try                                             '<------------- ADDED
        camEx.TakePicture
    Catch
        Log("TakePicture: " & LastException)
    End Try
End Sub

Sub Camera1_PictureTaken (Data() As Byte)
    Dim NombreFoto As String
    Dim ListaFotos As List
    Log("PictureTaken:" & Starter.SiteActual)
    .......
    .......
    ProgressBar1.Visible = False
    btnTakePicture.Enabled = False                          '<------------- ADDED
End Sub


Thank you all, I'll keep testing and asking any doubt.
 
Last edited:
Upvote 0
Top