unit Unit1; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls, FMX.Controls.Presentation, FMX.ScrollBox, FMX.Memo; type TForm1 = class(TForm) mmo1: TMemo; btn1: TButton; btn2: TButton; Button1: TButton; procedure btn2Click(Sender: TObject); procedure btn1Click(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.fmx} uses {$IFDEF ANDROID} Androidapi.JNI.GraphicsContentViewText, Androidapi.JNI.JavaTypes, Androidapi.JNI.App, Androidapi.JNI.Net, Androidapi.Helpers, ksAndroid.Helpers, {$ENDIF} System.Messaging; type TOpenFileCallback = reference to procedure(const ACode: Integer; const APath: string); TOpenFileEvent = procedure (const ACode: Integer; const APath: string) of object; const FILE_SELECT_CODE = 0; var FMessageChooserID: Integer = 0; FOpenFileCallback: TOpenFileCallback; FOpenFileEvent: TOpenFileEvent; FAction: JString; procedure HandleActivityMessage(const Sender: TObject; const M: TMessage); var LCode: Integer; FileName:string; URI: Jnet_Uri; begin if (not Assigned(M)) or (not (M is TMessageResultNotification)) or (TMessageResultNotification(M).RequestCode <> FILE_SELECT_CODE) then Exit; LCode := TMessageResultNotification(M).ResultCode; FileName := ''; {$IFDEF DEBUG} if LCode = TJActivity.JavaClass.RESULT_CANCELED then Log.d('---HandleActivityMessage-RESULT_CANCELED'); if LCode = TJActivity.JavaClass.RESULT_FIRST_USER then Log.d('---HandleActivityMessage-RESULT_FIRST_USER'); {$ENDIF} if LCode = TJActivity.JavaClass.RESULT_OK then begin if TMessageResultNotification(M).Value <> nil then begin URI := TMessageResultNotification(M).Value.getData; if URI <> nil then FileName := TAndroidHelperEx.FileFromUri(URI) {$IFDEF DEBUG} else Log.d('---HandleActivityMessage-URI:nil'); {$ENDIF} end else begin {$IFDEF DEBUG} Log.d('---HandleActivityMessage-Value:nil'); {$ENDIF} end; end; {$IFDEF DEBUG} Log.d('---HandleActivityMessage-ResultCode:%d-', [LCode]); {$ENDIF} if Assigned(FOpenFileCallback) then FOpenFileCallback(LCode, FileName); if Assigned(FOpenFileEvent) then FOpenFileEvent(LCode, FileName); if FMessageChooserID <> 0 then begin TMessageManager.DefaultManager.Unsubscribe(TMessageResultNotification, FMessageChooserID); FMessageChooserID := 0; FOpenFileCallback := nil; FOpenFileEvent := nil; end; end; function OpenFileDialog(AExt, ADir: string; AData: Jnet_Uri = nil; AOpenFileCallback: TOpenFileCallback = nil; AOpenFileEvent: TOpenFileEvent = nil): Boolean; var LIntent: JIntent; LType: string; begin Result := False; LType := ''; if (LType = '') or (AExt = '*.*') or (AExt = '*') then LType := '' else if Pos('/', AExt) > 0 then LType := AExt else LType := JStringToString(TAndroidHelperEx.GetMimeType(AExt)); if LType = '' then LType := '*/*'; LIntent := TJIntent.JavaClass.init(FAction); LIntent.setType(StringToJString(LType)); //Intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); 多选 if FAction.equals(TJIntent.JavaClass.ACTION_PICK) then //Intent.setData(TJImages_Media.JavaClass.EXTERNAL_CONTENT_URI) // 这个可以用来限定APP,比如这个就是 图库 //Intent.setData(TJContactsContract_Contacts.JavaClass.CONTENT_URI) // 联系人,不指定这个的话会直接取消 else begin LIntent.addCategory(TJIntent.JavaClass.CATEGORY_OPENABLE); if ADir <> '' then LIntent.setData(TAndroidHelperEx.UriParse(ADir)); end; if FMessageChooserID = 0 then begin FMessageChooserID := TMessageManager.DefaultManager.SubscribeToMessage(TMessageResultNotification, HandleActivityMessage); FOpenFileCallback := AOpenFileCallback; FOpenFileEvent := AOpenFileEvent; end; try //Result := TAndroidHelperEx.StartActivity(TJIntent.JavaClass.createChooser(Intent, StrToJCharSequence(ATitle)), FILE_SELECT_CODE) Result := TAndroidHelperEx.StartActivity(LIntent, FILE_SELECT_CODE); except raise Exception.Create('File Manager not found'); end; end; procedure TForm1.btn1Click(Sender: TObject); begin FAction := TJIntent.JavaClass.ACTION_PICK; OpenFileDialog('*/*', '', nil, procedure(const ACode: Integer; const APath: string) begin if APath <> '' then mmo1.Lines.Add(APath) else mmo1.Lines.Add(Format('Error code: %d', [ACode])); end ) end; procedure TForm1.btn2Click(Sender: TObject); begin FAction := TJIntent.JavaClass.ACTION_GET_CONTENT; OpenFileDialog('*/*', '', nil, procedure(const ACode: Integer; const APath: string) begin if APath <> '' then mmo1.Lines.Add(APath) else mmo1.Lines.Add(Format('Error code: %d', [ACode])); end ) end; procedure TForm1.Button1Click(Sender: TObject); begin FAction := TJIntent.JavaClass.ACTION_OPEN_DOCUMENT; OpenFileDialog('*/*', '', nil, procedure(const ACode: Integer; const APath: string) begin if APath <> '' then mmo1.Lines.Add(APath) else mmo1.Lines.Add(Format('Error code: %d', [ACode])); end ) end; end.