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

问题描述 投票:0回答:1

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

但是由于“安全”原因,我们无法将邮件应用程序作为 Intent 对象进行操作,并且我们不能简单地扫描“下载”文件夹以查找附件。

因此,让我们尝试注册一个文件 mimeType,这样至少用户可以点击“下载”文件夹中的文件,然后在“意图选择器”中选择我们的应用程序。

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

<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_IMAGES,似乎指的是平民在玩相机胶卷,并且没有 READ_MEDIA_TEXT 或 READ_MEDIA_HTML。

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

    <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 添加一个处理程序来接收 Intent:

@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 查看器和 Internet。

那么我们如何让我们的应用程序打开在“下载”文件夹中找到的文件?

android android-intent android-storage
1个回答
0
投票

更换:

            <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.xml 我们的应用程序可以读取文件

你不需要这些。其中两个(

INTERNAL
)甚至不存在。使用
ContentResolver
openInputStream()
InputStream
标识的内容上获得
Uri
,您将在
onCreate()
(如果正在创建新活动)或
onNewIntent()
(如果活动已经已存在,并且正在交付新的
Intent
)。

android:foregroundServiceType="location"

这是为了服务。对于

<activity>
来说毫无意义。

© www.soinside.com 2019 - 2024. All rights reserved.