简体   繁体   English

如何为Intent.ACTION_GET_CONTENT提供内容

[英]How to provide content for Intent.ACTION_GET_CONTENT

The web and stackoverflow contain several examples how to get a file from another Android app (eg, to use it as email attachment) using an ACTION_GET_CONTENT intent. web和stackoverflow包含几个示例,说明如何使用ACTION_GET_CONTENT意图从另一个Android应用程序获取文件(例如,将其用作电子邮件附件)。 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 (eg, for selecting an email attachment). 但是我需要实现什么类来创建为ACTION_GET_CONTENT事件提供内容的应用程序,例如我可以选择此应用程序(例如,用于选择电子邮件附件)。

Is a ContentProvider the right solution? ContentProvider是一个正确的解决方案吗? And what do I have to add to my AndroidManifest.xml? 我还需要添加到AndroidManifest.xml中?

After some hours of web search I found the following solution. 经过几个小时的网络搜索后,我找到了以下解决方案。

  1. Implement an Activity handling intents. 实现一个Activity处理意图。 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(); 
  2. Add the following to the Manifest: 将以下内容添加到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> 

starting from api level 18 incoming intent can also have EXTRA_ALLOW_MULTIPLE set to true and in this case you can send back in result more than one file. 从api级别18开始,传入的意图也可以将EXTRA_ALLOW_MULTIPLE设置为true,在这种情况下,您可以将结果发送回多个文件。 To do so you need to set it as ClipData: 为此,您需要将其设置为ClipData:

resultIntent.setClipData(clipData)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM