如何使用Flutter应用程序从文件管理器打开(音频)文件 [英] How to open (audio) file from file manager using Flutter app

查看:1337
本文介绍了如何使用Flutter应用程序从文件管理器打开(音频)文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户尝试从他/她的文件管理器中打开音频文件时,我想在以下在哪个应用程序中打开该文件"弹出窗口中向他/她显示我的应用程序.在从弹出窗口中选择我的应用程序之后,我想将文件路径传递给状态变量(currentSong).

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)he selects my app from the pop-up window, I want to pass file path into a state variable (currentSong).

我已经设法将以下IntentFilter添加到我的AndroidManifest.xml中.这应该会在弹出窗口中向用户正确显示我的应用程序:

I've already managed to add the following IntentFilter into my AndroidManifest.xml. This should correctly show the user my app in the pop-up window:

<intent-filter>
    <action android:name="android.intent.action.VIEW"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE"/>
    <data android:scheme="http" />
    <data android:scheme="content" />
    <data android:scheme="file" />
    <data android:mimeType="audio/*" />
    <data android:mimeType="application/ogg" />
    <data android:mimeType="application/x-ogg" />
    <data android:mimeType="application/itunes" />
</intent-filter>

但是现在我一无所有.如何获取文件路径并将其保存到Flutter中的状态变量currentSong中?

But now I'm just clueless. How to get the file path and save it into the state variable currentSong in Flutter?

推荐答案

您需要使用ChannelPlatform来做到这一点:

You need using ChannelPlatform to do that:

1. Android方面:

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

2.颤抖的一面:

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

演示:

示例:

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
        }
    }
}

颤振:

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();
}

这篇关于如何使用Flutter应用程序从文件管理器打开(音频)文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆