Android - 获取我的应用程序中选定的 pdf、doc、ppt 或 xls 的文件路径

标签 android android-intent open-with browsable

我正在开发一个 Android 应用程序。

我的目标:

如果用户打开 SDCard(或)dropbox(或)电子邮件等内任何文件夹中的 pdf、doc、ppt 或 xls 文件,他们必须导航到我的应用程序(在我的应用程序中,我会做一些根据他们选择的文件进行处理)。

我做了什么:

如果用户单击 PDF 文件,我可以获得文件路径,并且他们会导航到我的应用程序。为此,我编写了以下代码。

Code snippet:

在AndroidManifest.xml中,我添加了以下代码:

        <activity android:name="com.openwith.OpenwithActivity" >
            <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="file" />
                <data android:mimeType="*/*" />
                <data android:scheme="http" android:host="*" android:pathPattern=".*\\.pdf" />
                <data android:scheme="https" android:host="*" android:pathPattern=".*\\.pdf" />
                <data android:scheme="content" android:host="*" android:pathPattern=".*\\.pdf" />
                <data android:scheme="file" android:host="*" android:pathPattern=".*\\.pdf" />
            </intent-filter>
        </activity>

在 OpenwithActivity.java 中

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebView;

public class OpenwithActivity extends Activity {

        @SuppressLint("SetJavaScriptEnabled")
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_openwith);

            Intent intent = getIntent();
            Uri data = intent.getData();
            if (data != null) {
                  String filePath = data.getPath();
            } 
        }

}

我需要什么:

我只获取 pdf 文件的路径。如果我想获取doc、ppt和xls文件的路径,我需要在AndroidManifest.xml中添加什么?

我在网上搜索过。但我没有得到任何明确的文档(或)教程。 谁能帮我做到这一点吗?

最佳答案

为其他文件类型添加与为 pdf 所做的相同的 Intent 过滤器条目。

<data android:scheme="http" android:host="*" android:pathPattern=".*\\.ppt" />
<data android:scheme="https" android:host="*" android:pathPattern=".*\\.ppt" />
<data android:scheme="content" android:host="*" android:pathPattern=".*\\.ppt" />
<data android:scheme="file" android:host="*" android:pathPattern=".*\\.ppt" />
<data android:scheme="http" android:host="*" android:pathPattern=".*\\.xls" />
<data android:scheme="https" android:host="*" android:pathPattern=".*\\.xls" />
<data android:scheme="content" android:host="*" android:pathPattern=".*\\.xls" />
<data android:scheme="file" android:host="*" android:pathPattern=".*\\.xls" />
<data android:scheme="http" android:host="*" android:pathPattern=".*\\.doc" />
<data android:scheme="https" android:host="*" android:pathPattern=".*\\.doc" />
<data android:scheme="content" android:host="*" android:pathPattern=".*\\.doc" />
<data android:scheme="file" android:host="*" android:pathPattern=".*\\.doc" />

对于图像,应该这样做:

<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:mimeType="image/*" android:scheme="http" android:host="*" />
    <data android:mimeType="image/*" android:scheme="https" android:host="*" />
    <data android:mimeType="image/*" android:scheme="content" android:host="*" />
    <data android:mimeType="image/*" android:scheme="file" android:host="*" />
</intent-filter>

关于Android - 获取我的应用程序中选定的 pdf、doc、ppt 或 xls 的文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24304464/

相关文章:

android - 如何在默认浏览器或 webview 中打开按钮点击 url

android - Intent 打开谷歌身份验证器

Python:获取与文件关联的应用程序列表 (Linux)

android fragment popBackStack 没有 POP_BACK_STACK_INCLUSIVE

php - 使用 Retrofit 发送图像文件帖子?

java - 如何修复 Android Studio 4 启动时的崩溃?

android - 有什么方法可以在我的Flutter应用程序中打开链接URL?

android - 如何正确使用onVisibilityChanged()、onWindowVisibilityChanged()、onAttached/Detached()?

android - 将 MuPDF 集成为库项目 (Android)

visual-studio-2010 - 打开方式对话框不记得新添加的程序