找不到适合处理android.intent.action.OPEN_DOCUMENT意图的活动

33

我正在尝试使用安卓4.4的存储访问框架。

我开发了一个虚拟应用,在活动开始时会触发意图。

    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    startActivityForResult(intent, READ_REQUEST_CODE);

此外,我还开发了另一个虚拟应用程序,作为文件提供程序。

        <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.saf"
    android:versionCode="1"
    android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="19"
    android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.saf.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <provider
        android:name="com.example.saf.MyFileProvider"
        android:authorities="com.example.saf.documents"
        android:exported="@bool/is_kitkat"
        android:enabled="@bool/is_kitkat"
        android:grantUriPermissions="@bool/is_kitkat"
        android:permission="android.permission.MANAGE_DOCUMENTS">
        <intent-filter>
            <action android:name="android.content.action.DOCUMENTS_PROVIDER" />
        </intent-filter>
    </provider>

</application>

我已经实现了MyFileProvider类。

但是当我启动用户应用程序(触发意图的应用程序)时,我会收到以下错误:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.OPEN_DOCUMENT cat=[android.intent.category.OPENABLE] }

我只是按照 Android 开发者文档进行操作,不知道我可能做错了什么?

编辑: 这是我的最新清单文件。 还需要我对 MyFileProvider 进行“适当”的实现,即扩展 DocumentsProvider 吗?现在我可以在函数中只返回 null 吗?


你的模拟应用程序使用权限 android.permission.MANAGE_DOCUMENTS 吗? - Sherif elKhatib
@SherifelKhatib - 实际上它没有那个权限。但我已经添加了并检查过了。仍然是同样的错误。 - yashdosi
3个回答

58

添加一个mime类型,或者简单地使用intent.setType("*/*")

这似乎是一个已知问题setType()是必需的。

编辑:你还需要添加android:enabled="true",请注意,你应该从值中引用它而不是直接引用,以便只在>=kitkat时启用它。因此,在/res/values/中添加<bool name="atLeastKitKat">false</bool>,并在/res/values-v19/中添加<bool name="atLeastKitKat">true</bool>


它运行了!但我仍然看不到我的主机应用程序,它正在作为提供者工作。 - yashdosi
仍然无法看到我的作为提供者工作的主机应用程序。 - yashdosi

8

要打开所有文件,请添加以下行:

intent.setType("*/*") ;

如果您需要过滤显示图片,请添加以下行:

intent.setType("image/*");

-1

只需表达你的意图,就像这样:

     Intent intent = new Intent("android.intent.action.GET_CONTENT");
    ((Intent)intent).addCategory("android.intent.category.OPENABLE");
    ((Intent)intent).setType("*/*");

2
很多不必要的强制类型转换...你可以直接写new Intent("android.intent.action.GET_CONTENT").addCategory(Intent.CATEGORY_OPENABLE).setType("*/*");,得到相同的结果,操作更少。 - Bonatti

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接