Search code examples
androidandroid-intentandroid-contentprovider

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 (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).

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


Solution

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

    1. 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();
      
    2. 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>