Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("1")
Dim rp As RuntimePermissions
rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE) ' Implicit read capability if granted
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
Log($"PERMISSION_WRITE_EXTERNAL_STORAGE = ${Result}"$)
imageFolder = rp.GetSafeDirDefaultExternal("shared") 'shared folder as set in the manifest editor
Log("imagefolder: "&imageFolder)
If lastPicture.IsInitialized Then ImageView1.Bitmap = lastPicture
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)
File.Delete(myFolder,"camintentTest.jpg")
Dim p As Phone
Dim u As Object
Log("SDK Version: "&p.SdkVersion)
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
Msgbox("Camera is not available."&CRLF&LastException, "Camera ERROR")
End Try
End Sub
'result arrives here
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
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
Msgbox( LastException,"ERROR")
End Try
' copy to directory
Try
File.Copy(imageFolder,tempImageFile,myFolder,"camintentTest.jpg") ' <==== THIS IS THE WHOLE "TRICK" !!
' NO FILEPROVIDER OR EXTFILE NECESSARY !
If File.Exists(myFolder,"camintentTest.jpg") Then _
Msgbox("Image written to: camintentTest.jpg ","IMAGE SAVE")
Catch
Msgbox( LastException,"ERROR in copy file")
End Try
End If
If lastPicture.IsInitialized Then ImageView1.Bitmap = lastPicture
Return Null
End Sub
Sub CreateFileProviderUri (Dir As String, FileName As String) As Object
Dim fp As JavaObject
Dim context As JavaObject
context.InitializeContext
fp.InitializeStatic("android.support.v4.content.FileProvider")
Dim f As JavaObject
f.InitializeNewInstance("java.io.File", Array(Dir, FileName))
Log("Packagename: "&Application.PackageName)
Return fp.RunMethod("getUriForFile", Array(context, Application.PackageName & ".provider", f))
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