Android Question Camera2 - Save images and videos

mackrackit

Member
Licensed User
Longtime User
Hi,

I am trying to learn from this example

But I can not figure out hos to save a still or a video. When finished with a video the program returns "Video file saved with a size" no location. A still image does not either.

I am using a Galaxy S21 Ultra 5G, Android version 12.

Thank you,
Dave
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Add Log(rp.GetSafeDirDefaultExternal("")) to find the path.
This is your app specific folder.

The relevant codes are:
B4X:
cam.DataToFile(Data, VideoFileDir, "1.jpg") 'still image

'and

File.Copy(VideoFileDir, "temp-" & VideoFileName, VideoFileDir, VideoFileName)

If you want to let the user select the output folder then use the SaveAs code: https://www.b4x.com/android/forum/t...list-of-other-related-methods.129897/#content
 
Upvote 0

mackrackit

Member
Licensed User
Longtime User
Still no luck. No saved picture.


I added Log(rp.GetSafeDirDefaultExternal(""))
and
File.MakeDir(File.DirRootExternal, "CAM_DATA")
to Sub TakePicture to see if anything could be written. That is not working either.
returning the below log
B4X:
/storage/emulated/0/Android/data/b4a.example/files
Error: (ErrnoException) android.system.ErrnoException: open failed: ENOENT (No such file or directory)
Start success: true

The only changes to the example are in Sub TakePicture and added Write permission to the manifest.
B4X:
Sub TakePicture
    Try
        Log(rp.GetSafeDirDefaultExternal(""))
        rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
        SetState(openstate, True, VideoMode)
        Wait For(cam.FocusAndTakePicture(MyTaskIndex)) Complete (Data() As Byte)
        SetState(openstate, False, VideoMode)
        cam.DataToFile(Data, VideoFileDir, "1.jpg")
        File.Copy(VideoFileDir, "temp-" & VideoFileName, VideoFileDir, VideoFileName)
        Dim bmp As Bitmap = cam.DataToBitmap(Data)
        Log("Picture taken: " & bmp) 'ignore
        pnlBackground.SetVisibleAnimated(100, True)
        B4XImageView1.Bitmap = RotateJpegIfNeeded(bmp, Data)
        
        File.MakeDir(File.DirRootExternal, "CAM_DATA")
        
        Sleep(4000)
        pnlBackground.SetVisibleAnimated(500, False)
    Catch
        HandleError(LastException)
    End Try
    
End Sub

The Manifest
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="31"/>
<supports-screens android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" 
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
'End of default text.
AddPermission(android.permission.RECORD_AUDIO)
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
File.DirRootExternal
You do not have access to this folder with targetsdk=31
 
Upvote 1

mackrackit

Member
Licensed User
Longtime User
Thank both you for trying to help me, but I am still missing something.
I changed everything back to the downloaded example with the exception of Log(rp.GetSafeDirDefaultExternal("")). Then set the targetsdk to 26 and tried 28.
Using the file manager on my phone or tablet 'Android/data/b4a.example/files' does not exist, I only have 'Android/data' ?? Both devices I am testing on are Android 12.
B4X:
Logger connected to:  samsung SM-T220
--------- beginning of main
Copying updated assets files (10)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
LEVEL_3
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
** Activity (main) Pause event (activity is not paused). **
** Activity (main) Resume **
Start success: false
Start success: true
/storage/emulated/0/Android/data/b4a.example/files
Error: (ErrnoException) android.system.ErrnoException: open failed: ENOENT (No such file or directory)
Start success: true
** Activity (main) Pause event (activity is not paused). **
** Service (starter) Destroy (ignored)**
 
Upvote 0
Top