如何为Intent.ACTION_GET_CONTENT提供内容

Cla*_*lke 7 android android-intent android-contentprovider

web和stackoverflow包含几个示例,说明如何使用ACTION_GET_CONTENT意图从另一个Android应用程序获取文件(例如,将其用作电子邮件附件).但是我需要实现什么类来创建为ACTION_GET_CONTENT事件提供内容的应用程序,例如我可以选择此应用程序(例如,用于选择电子邮件附件).

ContentProvider是一个正确的解决方案吗?我还需要添加到AndroidManifest.xml中?

Cla*_*lke 16

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

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

    Uri resultUri = // the thing to return
    Intent result = new Intent();
    result.setData(resultUri);
    setResult(Activity.RESULT_OK, result);
    finish();
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将以下内容添加到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>
    
    Run Code Online (Sandbox Code Playgroud)