0

I want to implement something like ShellExcute() but on Android.

I want to use an Intent to open a file in an external app.

I think it works well when tested, but for xlsx, ppt, and pdf extensions, my default Google apps (Spreadsheets, Presentations, Drive PDF viewers) are not receiving the file.

There is no problem with images and galleries in Google apps.

So, if I get the MIME-Type and there is no app for it, my code throws an exception:

image

I can handle exceptions, but Google apps are not being displayed in the selection list, even if I set the MIME-Type to */*.

That's why I have a basic Google apps, which forces users to install third-party apps.

What do I have to do for Google apps to get my Intent?

When I open the corresponding file using ES File Explorer on my device, those Google apps come out well.

This problem occurs equally well when testing both my device and the emulator.

Below is the example code I tested:

uses
  Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNI.JavaTypes,
  Androidapi.Helpers,
  System.IOUtils,
  Androidapi.JNI.Webkit;

{$R *.fmx}

procedure TForm1.Button1Click(Sender: TObject);
var
  Intent: JIntent;
  filename: String;
  t: JString;
begin
  filename := Edit1.text;
  Intent := TJIntent.Create;

  // Get MIME Type
  t := TJMimeTypeMap.JavaClass.getSingleton.getMimeTypeFromExtension
        (StringToJString (StringReplace (TPath.GetExtension(filename), '.', '', [])));
  if t = nil then
    t := StringToJString('*/*');

  Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
  Intent.setDataAndType(StrToJURI(TPath.GetPublicPath + PathDelim + filename),  t);
  SharedActivity.startActivity(Intent);
end;
Vincent J
  • 1
  • 1
  • If ES File Explorer is able to trigger Google the way you want, but your app is not, then clearly ES is sending a different `Intent` than you are. There are tools for monitoring/analyzing Intents – Remy Lebeau Nov 15 '17 at 00:47
  • Try to use correct MIME type for Excel file https://stackoverflow.com/questions/974079/setting-mime-type-for-excel-document – Zamrony P. Juhara Nov 15 '17 at 00:48
  • Also try specifying a Category on the `Intent`, like `CATEGORY_OPENABLE`. Or use a different Action, like `ACTION_OPEN_DOCUMENT`. – Remy Lebeau Nov 15 '17 at 00:52
  • Thank you very much. I'll try it. Have a good day. – Vincent J Nov 15 '17 at 01:21

0 Answers0