Android实例-从照相机或图库获取照片(XE8+小米2)
结果:
1.如果要取本地相册的话,小米手机要注意一下,不能取网络相册。
操作:
1.两个 TButton (Button1 和 Button2) , 一个 TActionList(ActionList1) ,一个 TImage(Image1)。
2.Button1 的 stylelookup 选 择 cameratoolbutton , Button1 的 stylelookup 选择organizetoolbutton。
3.双击 ActionList1,在弹出的对话框中点击右键菜单中的new standard action,然后选择TakePhotoFromLibraryAction( 从图片库中选择照片)和TakePhotoFromCameraAction(通过相机拍摄照片),这样就加入了两个标准的 Action。
4.在 TakePhotoFromCameraAction1 的 onDidFinishTaking 事件中写如下代码:
Image1.Bitmap.Assign(Image);
同样,在 TakePhotoFromLibraryAction1 的 onDidFinishTaking 事件中写如下代码:
Image1.Bitmap.Assign(Image);
5.Button1 的 Action 设置为 TakePhotoFromCameraAction1,Button2 的 Action 设置为TakePhotoFromLibraryAction1。
实例代码:
1 unit Unit1; 2 3 interface 4 5 uses 6 System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, 7 FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, System.Actions, 8 FMX.ActnList, FMX.Objects, FMX.Controls.Presentation, FMX.StdCtrls, 9 FMX.MediaLibrary.Actions, FMX.StdActns; 10 11 type 12 TForm1 = class(TForm) 13 Button1: TButton; 14 Button2: TButton; 15 Image1: TImage; 16 ActionList1: TActionList; 17 TakePhotoFromLibraryAction1: TTakePhotoFromLibraryAction;//手动增加的Action 18 TakePhotoFromCameraAction1: TTakePhotoFromCameraAction;//手动增加的Action 19 procedure TakePhotoFromLibraryAction1DidFinishTaking(Image: TBitmap); 20 procedure TakePhotoFromCameraAction1DidFinishTaking(Image: TBitmap); 21 private 22 { Private declarations } 23 public 24 { Public declarations } 25 end; 26 27 var 28 Form1: TForm1; 29 30 implementation 31 32 {$R *.fmx} 33 {$R *.NmXhdpiPh.fmx ANDROID} 34 35 //来自手机照相功能 36 procedure TForm1.TakePhotoFromCameraAction1DidFinishTaking(Image: TBitmap); 37 begin 38 Image1.Bitmap.Assign(Image); 39 end; 40 41 //来自手机的本地相册 42 procedure TForm1.TakePhotoFromLibraryAction1DidFinishTaking(Image: TBitmap); 43 begin 44 Image1.Bitmap.Assign(Image); 45 end; 46 47 end.
欢迎关注我,一起进步!扫描下方二维码即可加我