如何为Intent.ACTION_GET_CONTENT内容何为、内容、Intent、ACTION_GET_CONTENT

2023-09-05 08:10:36 作者:输不起的感情

在网络和计算器包含几个示例如何使用ACTION_GET_CONTENT意图获取其它Android应用程序的文件(例如,使用它作为电子邮件附件)。但是,做什么样的班我要实现创造了ACTION_GET_CONTENT时提供的内容,如我可以选择这个应用程序(例如,用于选择电子邮件附件)的应用程序。

The web and stackoverflow contain several examples how to get a file from another Android app (e.g., to use it as email attachment) using an ACTION_GET_CONTENT intent. But what kind of class do I have to implement to create an application providing content for the ACTION_GET_CONTENT event such as I can choose this app (e.g., for selecting an email attachment).

时ContentProvider的正确的解决方案?而且我有什么要添加到我的Andr​​oidManifest.xml?

Is a ContentProvider the right solution? And what do I have to add to my AndroidManifest.xml?

推荐答案

几个小时的网络搜索后,我发现了以下解决方案。

After some hours of web search I found the following solution.

实施的活动处理的意图。在使用以下或更具体的code:

Implement an Activity handling intents. Within, use the following or more specific code:

Uri resultUri = // the thing to return
Intent result = new Intent();
result.setData(resultUri);
setResult(Activity.RESULT_OK, result);
finish();

添加以下的清单:

Add the following to the Manifest:

<activity
    android:name="ActivityName"
    android:label="Some label" >
    <intent-filter>
        <action android:name="android.intent.action.GET_CONTENT" />
        <category android:name="android.intent.category.OPENABLE" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="*/*" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.PICK" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="*/*" />
    </intent-filter>
</activity>