如何在 Delphi 10.3 平台上从 android 设备检索视频的缩略图?
How can i retrieve the thumbnails of a video from an android device in a Delphi 10.3 platform?
在 Delphi 10.3 下开发的移动应用程序中,我想显示我从设备的本地存储中选取的视频的缩略图。谁能分享在 Delphi.
中执行此操作的示例代码
根据@Dave Nottage 的建议进行编辑。
现在我只需要一种获取所选视频文件的绝对路径的方法。
procedure TForm1.OpenFileSelector(Sender: TObject; const APermissions: TArray<string>;
const AGrantResults: TArray<TPermissionStatus>);
var
Intent: JIntent;
begin
FMessageSubscriptionID := TMessageManager.DefaultManager.SubscribeToMessage(TMessageResultNotification,
HandleActivityMessage);
Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_PICK);
Intent.setType(StringToJString('video/*'));
Intent.setAction(TjIntent.JavaClass.ACTION_GET_CONTENT);
Intent.putExtra(TJIntent.JavaClass.EXTRA_ALLOW_MULTIPLE, true);
Tandroidhelper.Activity.startActivityForResult(Intent,0);
{$ENDIF}
end;
//-----------------------------
procedure TForm1.HandleActivityMessage(const Sender: TObject; const aMessage: TMessage);
// when message is received
begin
if aMessage is TMessageResultNotification then
HandleIntentAction(TMessageReceivedNotification(aMessage).Value);
end;
//-------------------------------------------
function TForm1.HandleIntentAction(const Data: JIntent): Boolean;
{code to get the details of the file selected from gallery}
//============
function LoadBitmapFromJBitmap(const ABitmap: TBitmap; const AJBitmap: JBitmap): Boolean;
var
LSurface: TBitmapSurface;
begin
LSurface := TBitmapSurface.Create;
try
Result := JBitmapToSurface(AJBitmap, LSurface);
if Result then
ABitmap.Assign(LSurface);
finally
LSurface.Free;
end;
end;
//---------------
procedure AssignVideoThumbnails(aPath: string);
var
LBitmap: JBitmap;
LFileName: string;
begin
// LFileName := TPath.Combine(TPath.GetDocumentsPath, 'SampleVideo.mp4'); {using the paramter aPath instead}
LBitmap := TJThumbnailUtils.JavaClass.createVideoThumbnail(StringToJString(aPath), TJImages_Thumbnails.JavaClass.MICRO_KIND); // 96 x 96
if Assigned(LBitmap) then
LoadBitmapFromJBitmap(Image1.Bitmap, LBitmap);
end;
//============
var
vURI: JString;
vFilePath: string;
begin
if Assigned(Data) then begin
vURI := Data.getData.getPath; //getPath returns URI path
vFilePath := ???? // I NEED THE ABSOLUTE PATH of the selected file here
AssignVideoThumbnails(vFilePath);
end;
end;
这是一个例子:
uses
System.IOUtils,
Androidapi.JNI.Media, Androidapi.Helpers, Androidapi.JNI.GraphicsContentViewText, Androidapi.JNI.Provider,
FMX.Surfaces, FMX.Helpers.Android;
function LoadBitmapFromJBitmap(const ABitmap: TBitmap; const AJBitmap: JBitmap): Boolean;
var
LSurface: TBitmapSurface;
begin
LSurface := TBitmapSurface.Create;
try
Result := JBitmapToSurface(AJBitmap, LSurface);
if Result then
ABitmap.Assign(LSurface);
finally
LSurface.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
LBitmap: JBitmap;
LFileName: string;
begin
LFileName := TPath.Combine(TPath.GetDocumentsPath, 'SampleVideo.mp4');
LBitmap := TJThumbnailUtils.JavaClass.createVideoThumbnail(StringToJString(LFileName), TJImages_Thumbnails.JavaClass.MICRO_KIND); // 96 x 96
LoadBitmapFromJBitmap(Image1.Bitmap, LBitmap);
end;
我使用 Deployment Manager 将示例视频部署到 ./assets/internal
的远程路径,在代码中等同于 TPath.GetDocumentsPath
。
LoadBitmapFromJBitmap
几乎如其所说:获取一个 JBitmap
并将其放入 TBitmap
:-)
在 Delphi 10.3 下开发的移动应用程序中,我想显示我从设备的本地存储中选取的视频的缩略图。谁能分享在 Delphi.
中执行此操作的示例代码根据@Dave Nottage 的建议进行编辑。 现在我只需要一种获取所选视频文件的绝对路径的方法。
procedure TForm1.OpenFileSelector(Sender: TObject; const APermissions: TArray<string>;
const AGrantResults: TArray<TPermissionStatus>);
var
Intent: JIntent;
begin
FMessageSubscriptionID := TMessageManager.DefaultManager.SubscribeToMessage(TMessageResultNotification,
HandleActivityMessage);
Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_PICK);
Intent.setType(StringToJString('video/*'));
Intent.setAction(TjIntent.JavaClass.ACTION_GET_CONTENT);
Intent.putExtra(TJIntent.JavaClass.EXTRA_ALLOW_MULTIPLE, true);
Tandroidhelper.Activity.startActivityForResult(Intent,0);
{$ENDIF}
end;
//-----------------------------
procedure TForm1.HandleActivityMessage(const Sender: TObject; const aMessage: TMessage);
// when message is received
begin
if aMessage is TMessageResultNotification then
HandleIntentAction(TMessageReceivedNotification(aMessage).Value);
end;
//-------------------------------------------
function TForm1.HandleIntentAction(const Data: JIntent): Boolean;
{code to get the details of the file selected from gallery}
//============
function LoadBitmapFromJBitmap(const ABitmap: TBitmap; const AJBitmap: JBitmap): Boolean;
var
LSurface: TBitmapSurface;
begin
LSurface := TBitmapSurface.Create;
try
Result := JBitmapToSurface(AJBitmap, LSurface);
if Result then
ABitmap.Assign(LSurface);
finally
LSurface.Free;
end;
end;
//---------------
procedure AssignVideoThumbnails(aPath: string);
var
LBitmap: JBitmap;
LFileName: string;
begin
// LFileName := TPath.Combine(TPath.GetDocumentsPath, 'SampleVideo.mp4'); {using the paramter aPath instead}
LBitmap := TJThumbnailUtils.JavaClass.createVideoThumbnail(StringToJString(aPath), TJImages_Thumbnails.JavaClass.MICRO_KIND); // 96 x 96
if Assigned(LBitmap) then
LoadBitmapFromJBitmap(Image1.Bitmap, LBitmap);
end;
//============
var
vURI: JString;
vFilePath: string;
begin
if Assigned(Data) then begin
vURI := Data.getData.getPath; //getPath returns URI path
vFilePath := ???? // I NEED THE ABSOLUTE PATH of the selected file here
AssignVideoThumbnails(vFilePath);
end;
end;
这是一个例子:
uses
System.IOUtils,
Androidapi.JNI.Media, Androidapi.Helpers, Androidapi.JNI.GraphicsContentViewText, Androidapi.JNI.Provider,
FMX.Surfaces, FMX.Helpers.Android;
function LoadBitmapFromJBitmap(const ABitmap: TBitmap; const AJBitmap: JBitmap): Boolean;
var
LSurface: TBitmapSurface;
begin
LSurface := TBitmapSurface.Create;
try
Result := JBitmapToSurface(AJBitmap, LSurface);
if Result then
ABitmap.Assign(LSurface);
finally
LSurface.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
LBitmap: JBitmap;
LFileName: string;
begin
LFileName := TPath.Combine(TPath.GetDocumentsPath, 'SampleVideo.mp4');
LBitmap := TJThumbnailUtils.JavaClass.createVideoThumbnail(StringToJString(LFileName), TJImages_Thumbnails.JavaClass.MICRO_KIND); // 96 x 96
LoadBitmapFromJBitmap(Image1.Bitmap, LBitmap);
end;
我使用 Deployment Manager 将示例视频部署到 ./assets/internal
的远程路径,在代码中等同于 TPath.GetDocumentsPath
。
LoadBitmapFromJBitmap
几乎如其所说:获取一个 JBitmap
并将其放入 TBitmap
:-)