Browse Source

add AllowMultiSelect to helper demo

KngStr 5 years ago
parent
commit
e15160d718
2 changed files with 81 additions and 30 deletions
  1. 27 9
      Demos/Android Helper/Unit1.fmx
  2. 54 21
      Demos/Android Helper/Unit1.pas

+ 27 - 9
Demos/Android Helper/Unit1.fmx

@@ -16,27 +16,36 @@ object Form1: TForm1
     Lines.Strings = (
       'ACTION_PICK'
       '  Delphi'#22270#24211#40664#35748#29992#30340#26041#24335#65292#21487#20197#36873#21462' '#25991#20214#65292#22270#29255#65292#35270#39057#65292#38899#39057#65292#32852#31995#20154#65292#31561#31561
+      '  Delphi'#40664#35748#30340#34892#20026#37324#65292#33719#21462#25991#20214#22320#22336#21644#20869#23481#26159#38752#28040#24687#30340#12290
+      '  '#27880#24847#65306'Delphi'#30340#33719#21462#22320#22336#30340#28040#24687#65292#22320#22336#26159#26412#22320#30340#65292#20063#23601#26159#35828#20182#20808#25335#36125#20102#25968#25454#22312#33258#24049#30340#32531#23384#21306#20570#20102#23384#20648
       ''
       'ACTION_GET_CONTENT'
       '  '#36739#20026#36890#29992#30340#36873#21462#26041#24335
       ''
       'ACTION_OPEN_DOCUMENT'
-      '  '#26032#24335#36873#21462#26041#24335#65292#20294#37096#20998'URI'#26080#27861#36716#25104#25991#20214#36335#24452
-      '  '#22914#65306#31995#32479#19979#36733#30446#24405#65292#23433#21331'10'#24320#22987#30340#25152#26377#25991#26723#31867#22411#30446#24405)
+      '  '#26032#24335#36873#21462#26041#24335#65292#20294#37096#20998'URI'#26080#27861#36716#25104#25991#20214#36335#24452#65292#36825#20010#26102#20505#23601#21482#33021#26159#33719#21462#25991#20214#25968#25454#20102
+      '  '#22914#65306#31995#32479#19979#36733#30446#24405#65292#23433#21331'10'#24320#22987#30340#25152#26377#25991#26723#31867#22411#30446#24405
+      ''
+      #20851#20110#22810#36873
+      '  ACTION_PICK '#19981#25903#25345#22810#36873
+      '  '#26377#20123#25163#26426#22810#36873#38656#35201#20320#20808#38271#25353#20854#20013#19968#20010#25991#20214#25165#20250#20986#29616#22810#36873#26694
+      ''
+      #20851#20110#26435#38480
+      '  '#23433#21331'10'#30340#26102#20505#65292'ACTION_PICK'#27809#26377#36816#34892#26102#30340#23384#20648#26435#38480#20063#21487#20197#33719#21462#22320#22336#65292#20854#20182#20004#20010#19981#34892)
     TextSettings.WordWrap = True
     Anchors = [akLeft, akTop, akRight, akBottom]
-    Position.Y = 172.000000000000000000
+    Position.Y = 186.000000000000000000
     Size.Width = 383.000000000000000000
-    Size.Height = 308.000000000000000000
+    Size.Height = 294.000000000000000000
     Size.PlatformDefault = False
     TabOrder = 0
-    Viewport.Width = 379.000000000000000000
-    Viewport.Height = 304.000000000000000000
+    Viewport.Width = 363.000000000000000000
+    Viewport.Height = 290.000000000000000000
   end
   object btn1: TButton
     Anchors = [akLeft, akTop, akRight]
     Position.X = 10.000000000000000000
-    Position.Y = 46.000000000000000000
+    Position.Y = 44.000000000000000000
     Size.Width = 361.000000000000000000
     Size.Height = 28.000000000000000000
     Size.PlatformDefault = False
@@ -47,7 +56,7 @@ object Form1: TForm1
   object btn2: TButton
     Anchors = [akLeft, akTop, akRight]
     Position.X = 10.000000000000000000
-    Position.Y = 87.000000000000000000
+    Position.Y = 83.000000000000000000
     Size.Width = 361.000000000000000000
     Size.Height = 28.000000000000000000
     Size.PlatformDefault = False
@@ -58,7 +67,7 @@ object Form1: TForm1
   object btn3: TButton
     Anchors = [akLeft, akTop, akRight]
     Position.X = 10.000000000000000000
-    Position.Y = 128.000000000000000000
+    Position.Y = 122.000000000000000000
     Size.Width = 361.000000000000000000
     Size.Height = 28.000000000000000000
     Size.PlatformDefault = False
@@ -77,4 +86,13 @@ object Form1: TForm1
     Text = 'FMXMediaLibrary.takeImageFromLibrary'
     OnClick = btn4Click
   end
+  object chkAllowMultiSelect: TCheckBox
+    Position.X = 11.000000000000000000
+    Position.Y = 161.000000000000000000
+    Size.Width = 358.000000000000000000
+    Size.Height = 19.000000000000000000
+    Size.PlatformDefault = False
+    TabOrder = 5
+    Text = 'chkAllowMultiSelect'
+  end
 end

+ 54 - 21
Demos/Android Helper/Unit1.pas

@@ -15,6 +15,7 @@ type
     btn2: TButton;
     btn3: TButton;
     btn4: TButton;
+    chkAllowMultiSelect: TCheckBox;
     procedure btn2Click(Sender: TObject);
     procedure btn1Click(Sender: TObject);
     procedure btn3Click(Sender: TObject);
@@ -46,8 +47,8 @@ uses
   FMX.Platform;
 
 type
-  TOpenFileCallback = reference to procedure(const ACode: Integer; const APath: string);
-  TOpenFileEvent = procedure (const ACode: Integer; const APath: string) of object;
+  TOpenFileCallback = reference to procedure(const ACode: Integer; const APath: string; const APathes: TStrings);
+  TOpenFileEvent = procedure (const ACode: Integer; const APath: string; const APathes: TStrings) of object;
 
 const
   FILE_SELECT_CODE = 0;
@@ -57,11 +58,15 @@ var
   FOpenFileEvent: TOpenFileEvent;
   FAction: JString;
 
+// https://stackoverflow.com/questions/31002388/android-intent-extra-allow-multiple-allows-only-single-picking
 procedure HandleActivityMessage(const Sender: TObject; const M: TMessage);
 var
+  I: Integer;
   LCode: Integer;
   FileName:string;
-  URI: Jnet_Uri;
+  LURI: Jnet_Uri;
+  LClipData: JClipData;
+  LList: TStringList;
 begin
   if (not Assigned(M)) or (not (M is TMessageResultNotification))
     or (TMessageResultNotification(M).RequestCode <> FILE_SELECT_CODE) then
@@ -69,6 +74,7 @@ begin
 
   LCode := TMessageResultNotification(M).ResultCode;
   FileName := '';
+  LList := nil;
 
   {$IFDEF DEBUG}
   if LCode = TJActivity.JavaClass.RESULT_CANCELED then
@@ -79,14 +85,30 @@ begin
 
   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}
+      with TMessageResultNotification(M).Value do begin
+        LURI := getData;
+        if LURI <> nil then
+          FileName := TAndroidHelperEx.FileFromUri(LURI)
+        else begin
+          LClipData := getClipData;
+          if LClipData <> nil then begin
+            LList := TStringList.Create;
+            for I := 0 to LClipData.getItemCount - 1 do begin
+              LURI := LClipData.getItemAt(I).getUri;
+              if LURI <> nil then
+                LList.Add(TAndroidHelperEx.FileFromUri(LURI))
+              {$IFDEF DEBUG}
+              else
+                Log.d('---HandleActivityMessage-getClipData.getItemAt(%d).getUri:nil', [I]);
+              {$ENDIF}
+            end;
+          end
+          {$IFDEF DEBUG}
+          else
+            Log.d('---HandleActivityMessage-getData & getClipData:nil');
+          {$ENDIF}
+        end;
+      end;
     end
     else begin
       {$IFDEF DEBUG}
@@ -100,9 +122,12 @@ begin
   {$ENDIF}
 
   if Assigned(FOpenFileCallback) then
-    FOpenFileCallback(LCode, FileName);
+    FOpenFileCallback(LCode, FileName, LList);
   if Assigned(FOpenFileEvent) then
-    FOpenFileEvent(LCode, FileName);
+    FOpenFileEvent(LCode, FileName, LList);
+
+  if Assigned(LList) then
+    FreeAndNil(LList);
 
   if FMessageChooserID <> 0 then begin
     TMessageManager.DefaultManager.Unsubscribe(TMessageResultNotification, FMessageChooserID);
@@ -112,7 +137,7 @@ begin
   end;
 end;
 
-function OpenFileDialog(AExt, ADir: string; AData: Jnet_Uri = nil; AOpenFileCallback: TOpenFileCallback = nil; AOpenFileEvent: TOpenFileEvent = nil): Boolean;
+function OpenFileDialog(AExt, ADir: string; AData: Jnet_Uri = nil; AAllowMultiSelect: Boolean = False; AOpenFileCallback: TOpenFileCallback = nil; AOpenFileEvent: TOpenFileEvent = nil): Boolean;
 var
   LIntent: JIntent;
   LType: string;
@@ -132,7 +157,9 @@ begin
 
   LIntent := TJIntent.JavaClass.init(FAction);
   LIntent.setType(StringToJString(LType));
-  //Intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); 多选
+
+  if AAllowMultiSelect then
+    LIntent.putExtra(TJIntent.JavaClass.EXTRA_ALLOW_MULTIPLE, true);
 
   if not FAction.equals(TJIntent.JavaClass.ACTION_PICK) then
     LIntent.addCategory(TJIntent.JavaClass.CATEGORY_OPENABLE);
@@ -165,10 +192,12 @@ begin
   //Intent.setData(TJImages_Media.JavaClass.EXTERNAL_CONTENT_URI) // 这个可以用来限定APP,比如这个就是 图库
   //Intent.setData(TJContactsContract_Contacts.JavaClass.CONTENT_URI) // 联系人,不指定这个的话会直接取消
   FAction := TJIntent.JavaClass.ACTION_PICK;
-  OpenFileDialog('*/*', '', TJImages_Media.JavaClass.EXTERNAL_CONTENT_URI,
-    procedure(const ACode: Integer; const APath: string) begin
+  OpenFileDialog('*/*', '', TJImages_Media.JavaClass.EXTERNAL_CONTENT_URI, chkAllowMultiSelect.IsChecked,
+    procedure(const ACode: Integer; const APath: string; const APathes: TStrings) begin
       if APath <> '' then
         mmo1.Lines.Add(APath)
+      else if Assigned(APathes) then
+        mmo1.Lines.AddStrings(APathes)
       else
         mmo1.Lines.Add(Format('Error code: %d', [ACode]));
     end
@@ -178,10 +207,12 @@ end;
 procedure TForm1.btn2Click(Sender: TObject);
 begin
   FAction := TJIntent.JavaClass.ACTION_GET_CONTENT;
-  OpenFileDialog('*/*', '', nil,
-    procedure(const ACode: Integer; const APath: string) begin
+  OpenFileDialog('*/*', '', nil, chkAllowMultiSelect.IsChecked,
+    procedure(const ACode: Integer; const APath: string; const APathes: TStrings) begin
       if APath <> '' then
         mmo1.Lines.Add(APath)
+      else if Assigned(APathes) then
+        mmo1.Lines.AddStrings(APathes)
       else
         mmo1.Lines.Add(Format('Error code: %d', [ACode]));
     end
@@ -191,10 +222,12 @@ end;
 procedure TForm1.btn3Click(Sender: TObject);
 begin
   FAction := TJIntent.JavaClass.ACTION_OPEN_DOCUMENT;
-  OpenFileDialog('*/*', '', nil,
-    procedure(const ACode: Integer; const APath: string) begin
+  OpenFileDialog('*/*', '', nil, chkAllowMultiSelect.IsChecked,
+    procedure(const ACode: Integer; const APath: string; const APathes: TStrings) begin
       if APath <> '' then
         mmo1.Lines.Add(APath)
+      else if Assigned(APathes) then
+        mmo1.Lines.AddStrings(APathes)
       else
         mmo1.Lines.Add(Format('Error code: %d', [ACode]));
     end