如何在安卓活动中打开某种文件类型?



我想让一个活动成为打开.json文件类型的一个选项。例如,当我在资源管理器中单击一个 .json 文件时,我希望在此活动中打开它(或有一个打开它的选项(。为了实现这一点,我尝试在清单中向我的活动添加一个路径模式、一个方案和一个主机。没有成功。资源管理器显示"没有应用程序可以打开此文件"。 在StackOverflow上搜索后,我尝试添加mimeType。还是没有成功。我已经从现有的StackOverflow问题中尝试了20多种不同的答案,但没有任何成功。以下是我尝试过的一些答案:这里,这里,这里。 这是我的活动:

<activity
android:name=".JsonActivity"
android:label="Json">
<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="https" />
<data android:scheme="content" />
<data android:scheme="file" />
<data android:host="*" />
<data android:pathPattern=".*\.json" />
</intent-filter>
</activity>

我做错了什么?我确保添加了WRITE_EXTERNAL_STORAGE和READ_EXTERNAL_STORAGE权限,以及类别可浏览和操作视图。

You need multiple intent filters to address different situation you want to handle.
Example 1, handle http requests without mimetypes:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
<data android:host="*" />
<data android:pathPattern=".*\.json" />
</intent-filter>
Handle with mimetypes, where the suffix is irrelevant:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
<data android:host="*" />
<data android:mimeType="application/json" />
</intent-filter>
Handle intent from a file browser app:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file" />`enter code here`
<data android:host="*" />
<data android:pathPattern=".*\.json" />
</intent-filter>

最新更新