【问题标题】:How to provide content for Intent.ACTION_GET_CONTENT如何为 Intent.ACTION_GET_CONTENT 提供内容
【发布时间】:2012-08-13 12:04:53
【问题描述】:

Web 和 stackoverflow 包含几个示例,如何使用 ACTION_GET_CONTENT Intent 从另一个 Android 应用获取文件(例如,将其用作电子邮件附件)。但是我必须实现什么样的类来创建一个为 ACTION_GET_CONTENT 事件提供内容的应用程序,例如我可以选择这个应用程序(例如,用于选择电子邮件附件)。

ContentProvider 是正确的解决方案吗?我必须在我的 AndroidManifest.xml 中添加什么?

【问题讨论】:

    标签: android android-intent android-contentprovider


    【解决方案1】:

    经过几个小时的网络搜索,我找到了以下解决方案。

    1. 实现一个 Activity 处理意图。在其中,使用以下或更具体的代码:

      Uri resultUri = // the thing to return
      Intent result = new Intent();
      result.setData(resultUri);
      setResult(Activity.RESULT_OK, result);
      finish();
      
    2. 将以下内容添加到清单中:

      <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>
      

    【讨论】:

    • 请注意在这种情况下它不起作用stackoverflow.com/questions/14151970/…
    • 在没有 ACTION_PICK 意图过滤器的情况下似乎对我有用。对于需要意图过滤器响应的情况有什么想法吗?似乎只适用于 KK 前后的 GET_CONTENT 意图。
    • 因此,如果我遇到崩溃,提示 android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.GET_CONTENT cat=[android.intent.category.OPENABLE]类型=.pdf }
    【解决方案2】:

    从 api 级别 18 开始,传入意图也可以将 EXTRA_ALLOW_MULTIPLE 设置为 true,在这种情况下,您可以将结果发送回多个文件。为此,您需要将其设置为 ClipData:

    resultIntent.setClipData(clipData)
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 2017-10-27
    相关资源
    最近更新 更多