Upload and Copy video from gallery to specific folder Firemonkey

  

I am trying to use Intent to copy videos from the gallery to the folder of my application. I was able to open the gallery by selecting the video and getting the path in the folder it is in. But I could not copy it to the folder of my application. I would like to select the video, copy it to my folder specifies my application. I am using Delphi Berlin UP2.
Any idea ?

unit unitPr;

interface

uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
Androidapi.Helpers, Androidapi.JNI.GraphicsContentViewText, FMX.Platform.Android,
Androidapi.Jni.JavaTypes, Androidapi.JNIBridge, FMX.StdCtrls,FMX.Helpers.Android,
IdURI,Androidapi.Jni.Net, FMX.Controls.Presentation, FMX.Edit,
IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP,
FGX.ApplicationEvents, Androidapi.JNI.Os, FMX.Platform,
FMX.MediaLibrary,
Androidapi.JNI.App, System.IOUtils, System.Messaging, FMX.Objects,Androidapi.JNI.Provider, FMX.Media;

type
TFormLoadVideo = class(TForm)
BitLocalizar: TButton;
Button1: TButton;
procedure BitLocalizarClick(Sender: TObject);
private
const ScanRequestCode = 0;
var FMessageSubscriptionID: Integer;
procedure HandleActivityMessage(const Sender: TObject; const M: TMessage);
function OnActivityResult(RequestCode, ResultCode: Integer; Data: JIntent): Boolean;
public
{ Public declarations }
// Data : Jnet_Uri ;

chooserIntent, Intent: JIntent;

end;

var
FormLoadVideo: TFormLoadVideo;

implementation

{$R *.fmx}

function TFormLoadVideo.OnActivityResult(RequestCode, ResultCode: Integer; Data:JIntent): Boolean;
begin

// Result := False;
TThread.Queue(nil,
procedure
var
uri : Jnet_Uri;
cur: JCursor;
proj: TJavaObjectArray<JString>;
column_index: Integer;
video,ar:string;
begin

TMessageManager.DefaultManager.Unsubscribe(TMessageResultNotification,
FMessageSubscriptionID);
FMessageSubscriptionID := 0;

if (RequestCode = 8) and (ResultCode = TJActivity.JavaClass.RESULT_OK) then
begin
uri := Data.getData;
proj := TJavaObjectArray<JString>.Create(3);
proj[0] := StringToJString(‘_data’);
proj[1] := StringToJString(‘_id’);
proj[2] := TJVideo_Thumbnails.JavaClass.DATA;

cur := TAndroidHelper.Context.getContentResolver.query(uri, proj, nil, nil, nil);
if cur <> nil then
try
video:=System.IOUtils.TPath.GetSharedDocumentsPath;
ar := System.IOUtils.TPath.Combine(video,’new_file.mp4′);
try
system.ioutils.tfile.copy(
StringReplace(JStringToString(uri.toString),’content’,’file’,[rfReplaceAll])
,ar);
except
on e:exception do
begin
ShowMessage(‘ p1 ‘+e.Message);
end;
end;

try
system.ioutils.tfile.copy(JStringToString(uri.getPath),ar);
except
on e:exception do
begin
ShowMessage(‘ p2 ‘+e.Message);
end;
end;
try
system.ioutils.tfile.copy(JStringToString(uri.getEncodedPath),ar);
except
on e:exception do
begin
ShowMessage(‘ p3 ‘+e.Message);
end;
end;

finally
cur.close;
end;
end;
end)

end;

procedure TFormLoadVideo.HandleActivityMessage(const Sender: TObject; const M: TMessage);
begin
if M is TMessageResultNotification then
OnActivityResult(TMessageResultNotification(M).RequestCode, TMessageResultNotification(M).ResultCode,
TMessageResultNotification(M).Value);
end;

procedure TFormLoadVideo.BitLocalizarClick(Sender: TObject);
var

Uri: Jnet_Uri;

begin
FMessageSubscriptionID :=
TMessageManager.DefaultManager.SubscribeToMessage(
TMessageResultNotification, HandleActivityMessage);

intent := TJIntent.Create;
intent.setAction(TJIntent.JavaClass.ACTION_GET_CONTENT);
Intent.setType (StringToJString (‘video/mp4’ )) ;

chooserIntent := TJIntent.JavaClass.createChooser(Intent, StrToJCharSequence(‘Choose media file’));
TAndroidHelper.Activity.startActivityForResult(chooserIntent, 8);

end;
end.

Comments are closed.