我们的一般问题是,我们需要平板电脑中的应用程序将邮件发送到另一台平板电脑,我们的应用程序在其中读取一个HTML格式的附件文件.

但出于"安全性"考虑,我们无法将邮件应用程序作为Intent对象进行操作,也无法简单地扫描"下载"文件夹以寻找附件.

因此,让我们try 注册一个文件mimeType,这样用户至少可以点击"下载"文件夹中的文件,并在"意图搜索器"中 Select 我们的应用程序.

首先告诉AndroidManifest.html,我们的应用程序可以读取文件,就像计算诞生以来的所有其他程序一样:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />

请注意,这些内容大多已被废弃(适用于Android 14),并请注意,现代替代品,例如Read_MEDIA_Ivana,似乎指的是玩相机卷轴的平民,并且没有Read_MEDIA_文本或Read_MEDIA_HTML.

现在告诉 list ,我们的应用程序始终是独特且顶级的:

    <activity
        android:name=".MainActivity"
        android:configChanges="orientation|screenSize|keyboard|keyboardHidden|navigation"
        android:theme="@style/AppTheme.NoActionBar"
        android:screenOrientation="sensorLandscape"
        tools:ignore="LockedOrientationActivity"
        android:foregroundServiceType="location"
        android:exported="true"
        android:launchMode="singleTop"
        >

现在,在前<intent-filter>个(这使我们成为一个应用程序)之后添加另一个过滤器,使我们成为mimeType处理程序:

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="file" />
            <data android:host="*" />
            <data android:mimeType="text/html" />
            <data android:pathPattern=".*\\.html" />
        </intent-filter>

现在向MainActivity.java添加一个处理程序以接收意图:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    if (intent != null && Intent.ACTION_VIEW.equals(intent.getAction())) {
        Uri fileUri = intent.getData();
        
        if (fileUri != null) {
            Log.e("TODO", "yo: " + fileUri);
            setGreenStatusBar(fileUri.getPath());
        }
    }
}

好的,现在我们将File. html附件邮寄到平板电脑,将其下载到"下载"文件夹,然后点击它.我们仅获得默认的HTML mimeType处理程序- Chrome、HTML Viewer和Internet.

那么我们如何才能让我们的应用程序打开下载文件夹中找到的文件呢?

推荐答案

取代:

            <data android:scheme="file" />
            <data android:host="*" />
            <data android:mimeType="text/html" />
            <data android:pathPattern=".*\\.html" />

与:

            <data android:scheme="content" />
            <data android:mimeType="text/html" />

file计划几年前基本上被放弃了.

首先告诉AndroidManifest.html我们的应用程序可以读取文件

您不需要其中任何一个.其中两个(INTERNAL个)甚至不存在.使用ContentResolveropenInputStream()Uri标识的内容上获得InputStream,您将在onCreate()(如果活动正在新创建)或onNewIntent()(如果活动已经存在并且正在获得新的Intent)中收到该内容.

android:foregroundServiceType="location"

这是为了服务.对于<activity>岁的人来说这是毫无意义的.

Android相关问答推荐

Android使用参数编写齐射后请求

如何允许我的应用程序在Android 10上运行,同时目标是API 33

错误:无法解析Symbol@style/Theme. Androidstudio in AndroidManifest.html for Kotlin Android Development''

打开平板电脑的下载文件夹中的文件,例如使用其mimeType将Intent发送到我们的应用程序

无法在Android Gradle中同步Chaquopy版本

Android在NavHost中的LazyColumn中编写约束布局:error - replace()在未放置的项目上调用

如何修复Google Play市场中有关已删除广告库的错误消息?

为什么可以';我不能直接在RecyclerView.ViewHolder中访问视图ID吗?

如何使用其他组件的位置来定位自定义弹出窗口

具有数据库和升级潜力的移动应用程序开发(Android)供朋友使用

如何在Android中使用嵌套的Recyclerview

在 jetpack compose 中交替使用 View.INVISIBLE

Jetpack Compose UI - 在 AlertDialog 中单击时按钮宽度会发生变化

Jetpack Compose 中的滑动按钮菜单

JCenter 是否永久关闭(10 月 31 日)?

Jetpack Compose Material3 禁用 ListItem

java.lang.IllegalArgumentException:与请求匹配的导航目的地 NavDeepLinkRequest

Kotlin 调用带参数的函数 Any is xxx ||任何 yyy 都不起作用

在 Android Studio 中替换字符串中的 "

如何在 Kotlin 的片段中制作图像列表?