我们的一般问题是,我们需要平板电脑中的应用程序将邮件发送到另一台平板电脑,我们的应用程序在其中读取一个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.
那么我们如何才能让我们的应用程序打开下载文件夹中找到的文件呢?