How to provide content for Intent.ACTION_GET_CONTENT
Asked Answered
I

2

7

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?

Indoaryan answered 13/8, 2012 at 12:4 Comment(0)
I
16

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>
    
Indoaryan answered 13/8, 2012 at 18:56 Comment(3)
Please see that it dose not work in this case #14152470Ansell
Seems to work for me without the ACTION_PICK intent filter. Any idea for what case that intent filter is required to respond? Seems to work with just the GET_CONTENT intent on both pre/post KK.Roadhouse
so if I'm getting a crash that saysandroid.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.GET_CONTENT cat=[android.intent.category.OPENABLE] typ=.pdf }Musician
B
1

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. To do so you need to set it as ClipData:

resultIntent.setClipData(clipData)
Buskirk answered 23/11, 2017 at 9:34 Comment(2)
what is the (clipData) ?Oscillograph
@Oscillograph developer.android.com/reference/android/content/ClipDataBuskirk

© 2022 - 2024 — McMap. All rights reserved.