4

I'm creating an app in Flutter to store any type of media, imagem, video, pdfs, etc. And I want to be able to receive share intents from other apps in the easiest way possible for the user.

So, my idea is to be able to simply receive the media without needing to open the app for the user to input something, they should simply select my app to receive the media and continue using the "source" app. Is that possible in flutter?

GusHill
  • 65
  • 2
  • 6
  • Yes, you can do that. In fact, this is what intent intends to do. You don't need your app to be running at all times to receive the data (through intent filter) and Flutter has nothing to do anything with it. Just open your `AndroidManifest.xml` file, add your `intent-filter`, there, its type and so on. – iDecode Aug 02 '21 at 15:28
  • I think I was not clear in my question, I am developing my app in flutter (not only because of the share intent), and the flutter package receive_sharing_intent seems to open the app every time. – GusHill Aug 02 '21 at 16:17
  • You can open the app -> receive data -> close the app, or go fully native for Android and handle the receiving through a service (not sure about iOS) – Mohammad Ersan Jan 17 '22 at 00:48

2 Answers2

3

this should work very well based on your requirement, receive_sharing_intent, just following the setup for android & ios and try the example:

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:receive_sharing_intent/receive_sharing_intent.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  StreamSubscription _intentDataStreamSubscription;
  List<SharedMediaFile> _sharedFiles;
  String _sharedText;

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

    // For sharing images coming from outside the app while the app is in the memory
    _intentDataStreamSubscription =
        ReceiveSharingIntent.getMediaStream().listen((List<SharedMediaFile> value) {
      setState(() {
        print("Shared:" + (_sharedFiles?.map((f)=> f.path)?.join(",") ?? ""));
        _sharedFiles = value;
      });
    }, onError: (err) {
      print("getIntentDataStream error: $err");
    });

    // For sharing images coming from outside the app while the app is closed
    ReceiveSharingIntent.getInitialMedia().then((List<SharedMediaFile> value) {
      setState(() {
        _sharedFiles = value;
      });
    });

    // For sharing or opening urls/text coming from outside the app while the app is in the memory
    _intentDataStreamSubscription =
        ReceiveSharingIntent.getTextStream().listen((String value) {
      setState(() {
        _sharedText = value;
      });
    }, onError: (err) {
      print("getLinkStream error: $err");
    });

    // For sharing or opening urls/text coming from outside the app while the app is closed
    ReceiveSharingIntent.getInitialText().then((String value) {
      setState(() {
        _sharedText = value;
      });
    });
  }

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

  @override
  Widget build(BuildContext context) {
    const textStyleBold = const TextStyle(fontWeight: FontWeight.bold);
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: <Widget>[
              Text("Shared files:", style: textStyleBold),
              Text(_sharedFiles?.map((f)=> f.path)?.join(",") ?? ""),
              SizedBox(height: 100),
              Text("Shared urls/text:", style: textStyleBold),
              Text(_sharedText ?? "")
            ],
          ),
        ),
      ),
    );
  }
}
Jim
  • 6,928
  • 1
  • 7
  • 18
2

Using Receive Sharing intent package, you can receive text & media files in closed as well as opened application.

Below code snippet to receive intent in closed application,

// For sharing images coming from outside the app while the app is closed
    ReceiveSharingIntent.getInitialMedia().then((List<SharedMediaFile> value) {
      setState(() {
        _sharedFiles = value;
      });
    });

You can go through this link for further understanding on how to receive intent in already opened & closed application.

ritsapp
  • 80
  • 2