Search code examples
androiddelphiandroid-intentfiremonkeydelphi-10.3-rio

Handling intent result of multiple selected files from the storage


I have this headache with android Intents. i'm trying to select multiple image from the storage. i made it work till the point to handle the results. my guess is that some sort of For loop of get the path of all files?. The Result in my code returns empty, i don't know what is the problem. my code so far :

//Button to launch activity : 
var
  Intent: JIntent;
begin
  FMessageSubscriptionID :=
    TMessageManager.DefaultManager.SubscribeToMessage
      (TMessageResultNotification, HandleActivityMessage);

  Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_PICK);
  Intent.setType(StringToJString('image/*'));
  Intent.setAction(TjIntent.JavaClass.ACTION_GET_CONTENT);
  Intent.putExtra(TJIntent.JavaClass.EXTRA_ALLOW_MULTIPLE, true);
  SharedActivity.startActivityForResult(Intent, 0);
end;

//Handling Result : 
procedure HandleActivityMessage(const Sender: TObject; const M:   TMessage);
function OnActivityResult(RequestCode, ResultCode: Integer; Data: JIntent): Boolean;

...

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

function TForm3.OnActivityResult(RequestCode, ResultCode: Integer;
      Data: JIntent): Boolean;
var
  filename : string;  list : Tstrings;
begin
   Result := False;
   TMessageManager.DefaultManager.Unsubscribe(
      TMessageResultNotification, FMessageSubscriptionID);
   FMessageSubscriptionID := 0;
         
   if RequestCode = ScanRequestCode then
   begin
     if ResultCode = TJActivity.JavaClass.RESULT_OK then
     begin
       if Assigned(Data) then
       begin
         // 
         filename := JStringToString(
                Data.getStringExtra(
                  StringToJString('RESULT'))
              );
         filename := JStringToString(
                Data.getStringExtra(
                  TJIntent.JavaClass.EXTRA_TEXT
                )
                );
        ShowMessage('Result : '+filename);
        memo1.Lines.Add(filename);
      end;

      Toast(Format(
       'Found %s format barcode:'#10'%s', [ScanFormat,
         ScanContent]), LongToast
       );
    end;
  end
  else if ResultCode = TJActivity.JavaClass.RESULT_CANCELED then
  begin
    ShowMessage('No');
  end;
Result := True;

end; end;

used IDE : Delphi 10.3.3 and Android 9


Solution

  • I found a solution. Here you go:

    // loop through all images
    JStringToString(Data.getClipData.getItemAt(i).getUri.toString