Android Question How to get the filename to save in database using camera with intent

Colin Evans

Active Member
Licensed User
Longtime User
Hi, I'm trying to work out how to get the filename of the saved camera image using the camera intent example but using the amended code I would have thought it would save in the 'imagefolder - being folder '/storage/emulated/0/Android/data/b4a.example3/files/shared' but it doesn't and the file saves as '/storage/emulated/0/camera/IMG_20211112_103349_0.jpg' I've dropped the code below but I just can't figure out a way of saving the image to the imagefolder I wish them to be in.

B4X:
#Region  Project Attributes 
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName: 
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #BridgeLogger: True
#End Region

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

Sub Process_Globals
    Private ion As Object
    Private const tempImageFile As String = "tempimage.jpg"
    Private lastPicture As Bitmap
    
End Sub

Sub Globals
    Private ImageView1 As ImageView
    Public imageFolder As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    imageFolder = Starter.rp.GetSafeDirDefaultExternal("shared")
    If lastPicture.IsInitialized Then ImageView1.Bitmap = lastPicture
    Log("folder " & imageFolder)
End Sub

Sub Button1_Click
    TakePicture
End Sub

Sub TakePicture
    Dim i As Intent
    i.Initialize("android.media.action.IMAGE_CAPTURE", "")
    File.Delete(imageFolder, tempImageFile)
    Log("folder " & imageFolder)
    Dim u As Object = Starter.provider.GetFileUri(tempImageFile)
    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 ion_Event (MethodName As String, Args() As Object) As Object
    If Args(0) = -1 Then
        Try
            Dim in As Intent = Args(1)
            If File.Exists(imageFolder, tempImageFile) Then
                lastPicture = LoadBitmapSample(imageFolder, tempImageFile, ImageView1.Width, ImageView1.Height)
                ImageView1.Bitmap = lastPicture
                File.Delete(imageFolder, tempImageFile) '<-----------------------
            Else If in.HasExtra("data") Then 'try to get thumbnail instead
                Dim jo As JavaObject = in
                lastPicture = jo.RunMethodJO("getExtras", Null).RunMethod("get", Array("data"))
            End If
        Catch
            Log(LastException)
        End Try
    End If
    If lastPicture.IsInitialized Then ImageView1.Bitmap = lastPicture
    Log("folder " & imageFolder)
    Return Null
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 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 Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
    
End Sub
 
Top