Sending Android Intents from Delphi (Part 2)

Some months ago I wrote a post with title “Sending Android Intents from Delphi (Part 1)”. In that post I promised to write a 2nd part… here’s it is!

This time I want to play a video deployed with my APK using an external player.

Some informations are available on the Internet, but to correctly know how to play the video I’ve to inspect my Nexus 5 with the LogCat while using the default file manager to start the default video player.

The app main form  is shown below

The main form
The main form

 

I’ve added an mp4 video file using the deployment manager.

This is the code under the button click event

procedure TForm1.Button1Click(Sender: TObject);
var
  Intent: JIntent;
  FileName, DestFileName: string;
  Data: Jnet_Uri;
  CompName: JComponentName;
const
  VIDEO_FILENAME = 'videoviewdemo.mp4';
begin
  FileName := System.IOUtils.TPath.GetDocumentsPath + PathDelim + VIDEO_FILENAME;
  DestFileName := TPath.GetSharedDownloadsPath + PathDelim + VIDEO_FILENAME;
  // Copy the file into a public path (you can use any public path)
  TFile.Copy(FileName, DestFileName, true);
  Data := TJnet_Uri.JavaClass.parse(StringToJString('file://' + DestFileName));
  Intent := TJIntent.Create;
  Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
  if CheckBox1.IsChecked then
  begin
    CompName := TJComponentName.JavaClass.init(StringToJString('android'),
      StringToJString('com.android.internal.app.ResolverActivity'));
    Intent.setComponent(CompName);
  end;
  Intent.setDataAndType(Data, StringToJString('video/mp4'));
  try
    MainActivity.startActivity(Intent);
  except
    on E: Exception do
    begin
      Label1.Text := E.Message;
    end;
  end;
end;

Using the checkbox you can force the OS to show the ResolverActivity.

see you

2 thoughts on “Sending Android Intents from Delphi (Part 2)

  1. Hi.
    When I try your code it gaves an error.

    I change it a little bit:

    procedure TForm4.Button1Click(Sender: TObject);
    var
    Intent: JIntent;
    FileName, DestFileName: string;
    Data: Jnet_Uri;
    CompName: JComponentName;
    const
    FileNameC = ‘recibo.pdf’ ;
    begin
    FileName := System.IOUtils.TPath.GetDocumentsPath + PathDelim + FileNameC;
    DestFileName := TPath.GetSharedDownloadsPath + PathDelim + FileNameC;

    // Copy the file into a public path (you can use any public path)
    TFile.Copy(FileName, DestFileName, true);
    Data := TJnet_Uri.JavaClass.parse(StringToJString(‘file://’ + DestFileName));
    Intent := TJIntent.Create;
    Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
    //I Try with this line commented and uncommented
    CompName := TJComponentName.JavaClass.init(StringToJString(‘android’),
    StringToJString(‘com.android.reader’));
    Intent.setComponent(CompName);
    Intent.setDataAndType(Data, StringToJString(‘application/pdf’));
    try
    SharedActivity.startActivity(Intent);
    except
    on E: Exception do
    begin
    ShowMessage(E.Message);
    end;
    end;
    end;

    The error that gaves me is (in both cases):

    Acces violation at address 40775BB0, accessing address DEADD00D

    Any clue?

    Salutti!

    Gabriele

  2. Hi.
    When I try your code it gaves an error.

    I change it a little bit:

    procedure TForm4.Button1Click(Sender: TObject);
    var
    Intent: JIntent;
    FileName, DestFileName: string;
    Data: Jnet_Uri;
    CompName: JComponentName;
    const
    FileNameC = ‘recibo.pdf’ ;
    begin
    FileName := System.IOUtils.TPath.GetDocumentsPath + PathDelim + FileNameC;
    DestFileName := TPath.GetSharedDownloadsPath + PathDelim + FileNameC;

    // Copy the file into a public path (you can use any public path)
    TFile.Copy(FileName, DestFileName, true);
    Data := TJnet_Uri.JavaClass.parse(StringToJString(‘file://’ + DestFileName));
    Intent := TJIntent.Create;
    Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
    //I Try with this line commented and uncommented
    CompName := TJComponentName.JavaClass.init(StringToJString(‘android’),
    StringToJString(‘com.android.reader’));
    Intent.setComponent(CompName);
    Intent.setDataAndType(Data, StringToJString(‘application/pdf’));
    try
    SharedActivity.startActivity(Intent);
    except
    on E: Exception do
    begin
    ShowMessage(E.Message);
    end;
    end;
    end;

    The error that gaves me is (in both cases):

    Acces violation at address 40775BB0, accessing address DEADD00D

    Any clue?

    Salutti!

    Gabriele

Comments are closed.