How to open (audio) file from file manager using Flutter app

前端 未结 1 1330
一向
一向 2021-01-14 05:55

When a user tries to open an audio file from his/her file manager I want to show him/her my app in the following \"In which app to open this file\" pop-up window. After (s)h

相关标签:
1条回答
  • 2021-01-14 06:42

    You need using ChannelPlatform to do that:

    1. Android side:

       Handle onCreate + onNewIntent to get bundle data from File Viewer App that share audio file's path
    

    2. Flutter side:

      get audio file's path and do something...
    

    Demo:

    Example:

    Android

    import android.content.Intent
    import android.os.Bundle
    import androidx.annotation.NonNull
    import io.flutter.embedding.android.FlutterActivity
    import io.flutter.embedding.engine.FlutterEngine
    import io.flutter.plugin.common.MethodChannel
    import io.flutter.plugins.GeneratedPluginRegistrant
    
    class MainActivity : FlutterActivity() {
        private val CHANNEL = "tinyappsteam.flutter.dev/open_file"
    
        var openPath: String? = null
        override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
            GeneratedPluginRegistrant.registerWith(flutterEngine)
            val channel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL)
            channel.setMethodCallHandler { call, result ->
                when (call.method) {
                    "getOpenFileUrl" -> {
                        result.success(openPath)
                    }
                    else -> result.notImplemented()
                }
            }
        }
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            handleOpenFileUrl(intent)
        }
    
        override fun onNewIntent(intent: Intent) {
            super.onNewIntent(intent)
            handleOpenFileUrl(intent)
        }
    
        private fun handleOpenFileUrl(intent: Intent?) {
            val path = intent?.data?.path
            if (path != null) {
                openPath = path
            }
        }
    }
    

    Flutter:

    import 'package:flutter/material.dart';
    import 'package:flutter/services.dart';
    
    class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
      static const platform =
          const MethodChannel('tinyappsteam.flutter.dev/open_file');
    
      String openFileUrl;
    
      @override
      void initState() {
        super.initState();
        getOpenFileUrl();
        // Listen to lifecycle events.
        WidgetsBinding.instance.addObserver(this);
      }
    
      @override
      void dispose() {
        super.dispose();
        WidgetsBinding.instance.removeObserver(this);
      }
    
      @override
      void didChangeAppLifecycleState(AppLifecycleState state) {
        if (state == AppLifecycleState.resumed) {
          getOpenFileUrl();
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: Text("Audio file: " + (openFileUrl != null ? openFileUrl : "Nothing!")),
          ),
        );
      }
    
      void getOpenFileUrl() async {
        dynamic url = await platform.invokeMethod("getOpenFileUrl");
        print("getOpenFileUrl");
        if (url != null && url != openFileUrl) {
          setState(() {
            openFileUrl = url;
          });
        }
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    0 讨论(0)
提交回复
热议问题