【问题标题】:React Native + Expo - Triggering an android intent to open locally stored files. (Android)React Native + Expo - 触发 android 意图以打开本地存储的文件。 (安卓)
【发布时间】:2019-05-15 10:53:11
【问题描述】:

我们有一个托管的 expo 应用程序,它使用 FileSystem.downloadAsync,这没关系。

我们有一个FlatList,它显示了下载文件的列表。

我们不想为太阳下的每个应用都构建文件查看器,这不是目标。

我们希望能够单击列表中的文件,并可能使用 Android Intent 系统向用户提供他们已经安装的可以处理文件类型的应用列表。

例如我们有类似的东西


    class FileBrowser extends Component {
        state = {
            files: [
                {
                    key: '1'
                    name: 'file.jpg',
                    sys_path: 'file://blah.jpg'
                }
            ]
        }

        openFile(item) {
            IntentLauncherAndroid.startActivityAsync(
               'android.intent.action.OPEN_DOCUMENT', {
                data: item.sys_path
            })
        }

        render() {
            return (
                <FlatList data={ this.state.files } renderItem={ (item) => <Button title={ item.name } onPress={ this.openFile(item) } />} />
            )
        }
    }

我们已经用尽了来自https://chromium.googlesource.com/android_tools/+/febed84a3a3cb7c2cb80d580d79c31e22e9643a5/sdk/platforms/android-23/data/activity_actions.txt 的意图列表

几乎唯一能到达那里的是android.intent.action.VIEW,但是它只是打开一个随机的应用程序列表,没有一个可以处理图像 - 我们还传入了 mime 类型,例如image/jpegimage/* 希望能过滤列表。


在 Android 上仅让用户在适当的应用程序中打开文件的正确方法是什么?

【问题讨论】:

    标签: android react-native android-intent expo


    【解决方案1】:

    使用 Expo SDK 34(我使用 expo 34.0.3,因为存在字体无法解决的问题),您可以这样做打开列表。但这不是打开列表的正确解决方案。因为用户必须做一些这样的工作。

    expo sharing api

                      await Sharing.shareAsync(
                            fileUri, 
                            {dialogTitle: 'share or copy your pdf via'}
                            ).catch(error =>{
                            console.log(error);
                        })
    

    如果您通过提供本地文件 URI 来执行此操作,您可以看到如下底部的操作表。然后用户可以选择复制到选项并将其粘贴到某个位置。在该用户可以打开文件之后,操作系统会询问将使用什么应用程序来打开您的文件。

    【讨论】:

      猜你喜欢
      • 2020-05-05
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-20
      相关资源
      最近更新 更多