KasemJaffer / receive_sharing_intent

A Flutter plugin that enables flutter apps to receive sharing photos, text and url from other apps.
Apache License 2.0
334 stars 396 forks source link

Receive intent with "Open With" on Android #250

Open jaroslawdabrowski opened 1 year ago

jaroslawdabrowski commented 1 year ago

Hi Team,

great job with this plugin. It is very helpful and I did not have any issues with it.

However, I would like to add my app to show on the "Open with" list for PDF files. I have added the following to my manifest: `

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="file" />
<data android:mimeType="application/pdf" />

`

Unfortunately your plugin does not handle this case. Uni link plugin hops in and I receive a path to the file in following format: "content://....". I am not able to open that link.

Is there any way to configure your plugin so that it works the same way as I would use the share option?

Thanks in advance for your help.

abhinavsinghring commented 1 year ago

I also wanted that my app can open pdf's in my custom built in pdf reader page so I did some testing and found that when opening file using <action android:name="android.intent.action.VIEW" /> android system send the uri of this file to intent and this package handle this type of intent as url/text type

To get the exact path of file using this I'm sharing my full code below so that you can reproduce.

AndroidManifest.xml

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="application/pdf" />
</intent-filter>

I have used uri_to_file package to convert Uri to File. This basically creates a folder in AppDirectory and creates a temporary copy file there and returns its path.

MainAuthPage.dart

import 'package:receive_sharing_intent/receive_sharing_intent.dart';
import 'package:uri_to_file/uri_to_file.dart';
import 'dart:io';

class MainAuthPage extends StatefulWidget {
  const MainAuthPage({super.key});

  @override
  State<MainAuthPage> createState() => _MainAuthPageState();
}

class _MainAuthPageState extends State<MainAuthPage> {

  StreamSubscription? _intentDataStreamSubscription;

  @override
  void initState(){
    super.initState();

    // For sharing or opening urls/text coming from outside the app while the app is in the memory
    _intentDataStreamSubscription =
      ReceiveSharingIntent.getTextStream().listen((String? value) async {
      if (value != null) {
        debugPrint('path of file: ${Uri.parse(value).path}'); 
        File file = await toFile(value);
        Get.to(
          ()=> PdfReaderPage(
            url: file.path,
            name: file.path.split('/').last.split('.').first,
            isFile: true,
          ),
          transition: Transition.cupertino,
          duration: const Duration(milliseconds: 500),
        );
      }
    }, onError: (err) {
      debugPrint("getLinkStream error: $err");
    });

    // For sharing or opening urls/text coming from outside the app while the app is closed
    ReceiveSharingIntent.getInitialText().then((String? value) async {
      if (value != null) {
        debugPrint('path of file: ${Uri.parse(value).path}'); 
        File file = await toFile(value);
        Get.to(
          ()=> PdfReaderPage(
            url: file.path,
            name: file.path.split('/').last.split('.').first,
            isFile: true,
          ),
          transition: Transition.cupertino,
          duration: const Duration(milliseconds: 500),
        );
      }
    });
  }

  @override
  void dispose(){
    super.dispose();
    _intentDataStreamSubscription?.cancel();
  }