gpt4 book ai didi

android - 如何在 Delphi Android 应用程序中调用原生相机?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:17:13 30 4
gpt4 key购买 nike

我正在使用 Delphi 10.3 Community Edition 编写一个简单的 Android 应用程序并尝试调用 native 设备摄像头,但出现错误。

我正在关注官方 Delphi guide :

On the Form Designer, select the button (for taking a photo). In the Object Inspector, select the drop-down list for the Action property. Select New Standard Action | Media Library | TTakePhotoFromCameraAction:

On the Events tab, expand the Action node, and then double-click the OnDidFinishTaking event.

Add the following code to the OnDidFinishTaking event handler:

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

This code assigns a picture taken from the mobile device camera to the Bitmap property of the TImage component.

我已经验证了项目 |选项 |使用权限 - 相机设置设置为 true。我也在请求启动应用程序所需的权限。在 Debug 或 Release 中运行没有区别。

但是有一个问题。单击按钮时出现以下错误消息:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.PackageItemInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference.

这是我为最简单的测试应用编写的代码:

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

type
TForm1 = class(TForm)
Button1: TButton;
ActionList1: TActionList;
TakePhotoFromCameraAction1: TTakePhotoFromCameraAction;
procedure FormCreate(Sender: TObject);
private
procedure PermissionRequestResult(Sender: TObject; const APermissions: TArray<string>; const AGrantResults: TArray<TPermissionStatus>);
procedure DisplayRationale(Sender: TObject; const APermissions: TArray<string>; const APostRationaleProc: TProc);
end;

var
Form1: TForm1;

implementation
uses
{$IFDEF ANDROID}
Androidapi.Helpers,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.Os,
{$ENDIF}
FMX.DialogService;

{$R *.fmx}

procedure TForm1.PermissionRequestResult(Sender: TObject; const APermissions: TArray<string>; const AGrantResults: TArray<TPermissionStatus>);
begin
// 3 permission involved
if (Length(AGrantResults) = 3)
and (AGrantResults[0] = TPermissionStatus.Granted)
and (AGrantResults[1] = TPermissionStatus.Granted)
and (AGrantResults[2] = TPermissionStatus.Granted) then
else
ShowMessage('Required permission has not been granted') ;
end;

procedure TForm1.DisplayRationale(Sender: TObject; const APermissions: TArray<string>; const APostRationaleProc: TProc);
begin
TDialogService.ShowMessage('Need to access the camera',
procedure(const AResult: TModalResult)
begin
APostRationaleProc;
end);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
permCam, permRead, permWrite: string;
begin
// Request permissions
permCam := JStringToString(TJManifest_permission.JavaClass.CAMERA);
permRead := JStringToString(TJManifest_permission.JavaClass.READ_EXTERNAL_STORAGE);
permWrite := JStringToString(TJManifest_permission.JavaClass.WRITE_EXTERNAL_STORAGE);
PermissionsService.RequestPermissions([permCam, permRead, permWrite], PermissionRequestResult, DisplayRationale);
end;

end.

如何让原生相机TTakePhotoFromCameraAction生效?

最佳答案

检查项目选项 > 权利列表 > 安全文件共享选项是否设置为 true

关于android - 如何在 Delphi Android 应用程序中调用原生相机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53829324/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com