Delphi/FireMonkey: Cannot set Android camera to work

  

I’m trying to reproduce the AccessCameraApp.dproj app but it does not work for the TakePhotoFromCameraAction1, giving me the following error:
java.lang.NullPointerException: Attempt to invoke virtual method
‘android.content.res.XmlResourceParser
android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)’ on a null object reference.
The odd thing is that the Library button works perfectly…
The form looks like this:
Form1
and the code is as follows:
unit Unit1;

interface

uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, System.Permissions,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls, System.Actions,
FMX.ActnList, FMX.StdActns, FMX.MediaLibrary.Actions, FMX.Objects, FMX.Graphics,
FMX.Controls.Presentation;

type
TForm1 = class(TForm)
Image1: TImage;
Button1: TButton;
ActionList1: TActionList;
Button2: TButton;
TakePhotoFromLibraryAction1: TTakePhotoFromLibraryAction;
TakePhotoFromCameraAction1: TTakePhotoFromCameraAction;
procedure TakePhotoFromCameraAction1DidFinishTaking(Image: TBitmap);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);

private
{ Private declarations }
procedure DisplayRationale(Sender: TObject; const APermissions: TArray<string>; const APostRationaleProc: TProc);
procedure TakePicturePermissionRequestResult(Sender: TObject; const APermissions: TArray<string>; const AGrantResults: TArray<TPermissionStatus>);

private const
PermissionCamera = ‘android.permission.CAMERA’;
PermissionReadExternalStorage = ‘android.permission.READ_EXTERNAL_STORAGE’;
PermissionWriteExternalStorage = ‘android.permission.WRITE_EXTERNAL_STORAGE’;

public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.Button1Click(Sender: TObject);
begin
TAG := 1;
PermissionsService.RequestPermissions([PermissionCamera, PermissionReadExternalStorage, PermissionWriteExternalStorage], TakePicturePermissionRequestResult, DisplayRationale);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
TAG := 2;
PermissionsService.RequestPermissions([PermissionCamera, PermissionReadExternalStorage, PermissionWriteExternalStorage], TakePicturePermissionRequestResult, DisplayRationale);
end;

procedure TForm1.DisplayRationale(Sender: TObject; const APermissions: TArray<string>; const APostRationaleProc: TProc);
begin
APostRationaleProc;
end;

procedure TForm1.TakePicturePermissionRequestResult(Sender: TObject; const APermissions: TArray<string>; const AGrantResults: TArray<TPermissionStatus>);
begin
if (Length(AGrantResults) = 3) and (AGrantResults[0] = TPermissionStatus.Granted) and (AGrantResults[1] = TPermissionStatus.Granted) and (AGrantResults[2] = TPermissionStatus.Granted) then
begin
if TAG = 1
then TakePhotoFromCameraAction1.Execute
else TakePhotoFromLibraryAction1.Execute;
end
else
ShowMessage(‘Cannot take a photo because the required permissions are not all granted’);
end;

procedure TForm1.TakePhotoFromCameraAction1DidFinishTaking(Image: TBitmap);
begin
Image1.Bitmap := Image;
end;

end.

Comments are closed.