Android Question [SOLVED] Intent based camera crashes when back button is tapped.

rleiman

Well-Known Member
Licensed User
Longtime User
Greetings,

I'm using the intent based camera and discovered the app crashes if the user taps the back button on their phone when the camera app is displayed on the screen.

Everything else works. In our app the user taps a camera button. the intend based camera takes a photo, then returns back to the original screen if the user taps the Ok button on the camera app. I also checked the folder for the photo and it's there.

I also tried this with other camera apps and the same crash happens. No error messages are displayed on the screen so I ran the app so it would record the logs which I'm listing here.

All help will be appreciated.

Thanks.

B4X:
Logger connected to:  samsung SM-G960U1
--------- beginning of crash
--------- beginning of main
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
The path: /storage/emulated/0/DCIM
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Service (starter) Destroy (ignored)**
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
The path: /storage/emulated/0/DCIM
main_ion_event (java line: 966)
java.io.FileNotFoundException: /storage/emulated/0/Android/data/synventive.ebb.ui/files/shared/tempimage.jpg (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:231)
    at java.io.FileInputStream.<init>(FileInputStream.java:165)
    at anywheresoftware.b4a.objects.streams.File.OpenInput(File.java:214)
    at anywheresoftware.b4a.objects.streams.File.Copy(File.java:340)
    at synventive.ebb.ui.main._ion_event(main.java:966)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at anywheresoftware.b4j.object.JavaObject$1.invoke(JavaObject.java:237)
    at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
    at $Proxy0.ResultArrived(Unknown Source)
    at anywheresoftware.b4a.BA$4.run(BA.java:568)
    at anywheresoftware.b4a.BA.setActivityPaused(BA.java:442)
    at synventive.ebb.ui.main$ResumeMessage.run(main.java:306)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:6986)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
Copying updated assets files (7)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
The path: /storage/emulated/0/DCIM
Error occurred on line: 590 (Main)
java.io.FileNotFoundException: /storage/emulated/0/Android/data/synventive.ebb.ui/files/shared/tempimage.jpg (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:231)
    at java.io.FileInputStream.<init>(FileInputStream.java:165)
    at anywheresoftware.b4a.objects.streams.File.OpenInput(File.java:214)
    at anywheresoftware.b4a.objects.streams.File.Copy(File.java:340)
    at synventive.ebb.ui.main._ion_event(main.java:1175)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at anywheresoftware.b4j.object.JavaObject$1.invoke(JavaObject.java:237)
    at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
    at $Proxy0.ResultArrived(Unknown Source)
    at anywheresoftware.b4a.BA$4.run(BA.java:568)
    at anywheresoftware.b4a.BA.setActivityPaused(BA.java:442)
    at synventive.ebb.ui.main$ResumeMessage.run(main.java:306)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:6986)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
** Activity (main) Resume **
 

DonManfred

Expert
Licensed User
Longtime User
java.io.FileNotFoundException: /storage/emulated/0/Android/data/synventive.ebb.ui/files/shared/tempimage.jpg (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:
231)
at java.io.FileInputStream.<init>(FileInputStream.java:
165)
at anywheresoftware.b4a.objects.streams.File.OpenInput(
File.java:214)
at anywheresoftware.b4a.objects.streams.File.Copy(
File.java:340)
at synventive.ebb.ui.main._ion_event(main.java:
966)

I guess synventive.ebb.ui is NOT the Cameraapp but yours

where is the relevant code which runs in your app when the user presses back?

The error looks like you don´t care about the result (successfull or not); you use try to USE it...

Hard to say without seeing the code
 
Last edited:
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
I guess synventive.ebb.ui is NOT the Cameraapp but yours

where is the relevant code which runs in your app when the user presses back?

The error looks like you don´t care about the result (successfull or not); you use try to USE it...

Hard to say without seeing the code
Hi Don,

The same thing happens with the sample code that came with the library. At present, the people I'm making the app for are ok with that but it would be nicer if the user could use the back button. This also happens with any other camera app that is installed on the phone.

Is there a way to detect the back button being pressed when control is returned to the app? If so, where should the code be placed?
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
The Error does NOT happen inside the intent based camera. The error happens in YOUR APP.

Adapt your code to check the result so it´ll not crash...

AGAIN: Hard to help without seeing the code you are using!
Hi Don,

I made this smaller app using the same coding and it also crashes the same way. Thanks for your help. :)

B4X:
#Region  Project Attributes
    #ApplicationLabel: Emad Camera
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

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.

    Private strShared As String
    Private kvs As KeyValueStore

    Private ion As Object
    Private const tempImageFile As String = "tempimage.jpg"
    Private imageFolder As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Main")

'    CallSubDelayed(TheCamera,"Activity_Create")

    InitializeObjects
    SetupSettingsDatabase

    If FirstTime Then
        imageFolder = Starter.rp.GetSafeDirDefaultExternal("shared") 'shared folder as set in the manifest editor
    End If
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

' Misc sub routines.
'-------------------
Sub SetupSettingsDatabase
    
'    kvs.DeleteAll
    
    If kvs.ContainsKey("FileNumberCount") = False Then ' This setting needs to be created.
        kvs.Put("FileNumberCount", 1)
    End If

End Sub

Sub InitializeObjects
    
    strShared = Starter.rp.GetSafeDirDefaultExternal("Settings")
    kvs.Initialize(strShared, "Settings")
End Sub

' Misc. sub routines from the forum.
'-----------------------------------
Sub GetBA As Object
    Dim jo As JavaObject
    Dim cls As String = Me
    cls = cls.SubString("class ".Length)
    jo.InitializeStatic(cls)
    Return jo.GetField("processBA")
End Sub

Sub ShowCamera
    Dim i As Intent
    i.Initialize("android.media.action.IMAGE_CAPTURE", "")
    Dim p As Phone
    Dim u As Object
    If p.SdkVersion < 24 Then
        Dim uri As Uri
        uri.Parse("file://" & File.Combine(imageFolder, tempImageFile))
        u = uri
    Else
        u = CreateFileProviderUri(imageFolder, tempImageFile)
    End If
    i.PutExtra("output", u) 'the image will be saved to this path
    Try
        StartActivityForResult(i)
    Catch
        ToastMessageShow("Camera is not available.", True)
        Log(LastException)
    End Try
End Sub

Sub StartActivityForResult(i As Intent)
    
    Dim jo As JavaObject = GetBA
    ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
    jo.RunMethod("startActivityForResult", Array As Object(ion, i))
End Sub

Sub CreateFileProviderUri (Dir As String, FileName As String) As Object
    Dim FileProvider As JavaObject
    Dim context As JavaObject
    context.InitializeContext
    FileProvider.InitializeStatic("android.support.v4.content.FileProvider")
    Dim f As JavaObject
    f.InitializeNewInstance("java.io.File", Array(Dir, FileName))
    Return FileProvider.RunMethod("getUriForFile", Array(context, Application.PackageName & ".provider", f))
End Sub

'result arrives here
Sub ion_Event (MethodName As String, Args() As Object) As Object
    
    Dim intFileCount As Int = kvs.Get("FileNumberCount")

    File.Copy(imageFolder, tempImageFile, GetExternalStoragePublicDirectory("DCIM") & _
        "/Camera", "Synventive" & intFileCount & ".jpg")
        
    kvs.Put("FileNumberCount", intFileCount +1)
        
    File.Delete(imageFolder, tempImageFile)

    Return Null
End Sub

Sub GetExternalStoragePublicDirectory (name As String) As String
    Dim jo As JavaObject
    jo.InitializeStatic("android.os.Environment")
    Dim path As Object = jo.RunMethod("getExternalStoragePublicDirectory", Array(name))
    If path = Null Or File.Exists(path, "") = False Then
        Log("No such folder.")
        Return ""
    End If
    Log("The path: " & path)
    Return path
End Sub

' Event handlers.
'----------------
Sub ButtonOpenCamera_Click
    
    If Starter.rp.Check(Starter.rp.PERMISSION_CAMERA) And Starter.rp.Check( Starter.rp.PERMISSION_WRITE_EXTERNAL_STORAGE) Then
        ShowCamera
    Else
        Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_CAMERA)
        Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    End If
End Sub

Sub ButtonExit_Click
    
    Dim i As Intent
    i.Initialize(i.ACTION_MAIN, "")
    i.AddCategory("android.intent.category.HOME")
    i.Flags = 0x10000000
    StartActivity(i)
End Sub

This is the log text for the app.

B4X:
Logger connected to:  samsung SM-G960U1
--------- beginning of main
Copying updated assets files (2)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
sending message to waiting queue (activity_permissionresult)
running waiting messages (2)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
The path: /storage/emulated/0/DCIM
Error occurred on line: 144 (Main)
java.io.FileNotFoundException: /storage/emulated/0/Android/data/b4a.example/files/shared/tempimage.jpg (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:231)
    at java.io.FileInputStream.<init>(FileInputStream.java:165)
    at anywheresoftware.b4a.objects.streams.File.OpenInput(File.java:214)
    at anywheresoftware.b4a.objects.streams.File.Copy(File.java:340)
    at b4a.example.main._ion_event(main.java:676)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at anywheresoftware.b4j.object.JavaObject$1.invoke(JavaObject.java:237)
    at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
    at $Proxy0.ResultArrived(Unknown Source)
    at anywheresoftware.b4a.BA$4.run(BA.java:568)
    at anywheresoftware.b4a.BA.setActivityPaused(BA.java:442)
    at b4a.example.main$ResumeMessage.run(main.java:306)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:6986)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
** Activity (main) Resume **
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Hi rleiman.

What DonManfred is trying to tell you is that your code expects a picture from the camera intent. If you tap the back button, there's no picture, and you have to check in your code if the intent returns a picture or not.

The error you're getting is clear:
ava.io.FileNotFoundException: /storage/emulated/0/Android/data/b4a.example/files/shared/tempimage.jpg (No such file or directory)

So you're trying to delete tempimage.jpg, but it doesn't exist.
File.Delete(imageFolder, tempImageFile)

You're even trying to copy a file that doesn't exist:
File.Copy(imageFolder, tempImageFile, GetExternalStoragePublicDirectory("DCIM") & _
"/Camera", "Synventive" & intFileCount & ".jpg")
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi rleiman.

What DonManfred is trying to tell you is that your code expects a picture from the camera intent. If you tap the back button, there's no picture, and you have to check in your code if the intent returns a picture or not.

The error you're getting is clear:


So you're trying to delete tempimage.jpg, but it doesn't exist.


You're even trying to copy a file that doesn't exist:
Hi José,

Thanks so much for the helpful hint! :D

Here's the code that's now working and no crash! ;)

B4X:
Sub ion_Event (MethodName As String, Args() As Object) As Object
    
    If File.Exists(imageFolder, tempImageFile) Then
        Dim intFileCount As Int = kvs.Get("FileNumberCount")

        File.Copy(imageFolder, tempImageFile, GetExternalStoragePublicDirectory("DCIM") & _
        "/Camera", "Synventive" & intFileCount & ".jpg")
        
        kvs.Put("FileNumberCount", intFileCount +1)
        
        File.Delete(imageFolder, tempImageFile)
    End If

    Return Null
End Sub

Again thanks.
 
Upvote 0
Top