How to provide content for Intent.ACTION_GET_CONTE

2020-06-16 05:39发布

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?

2条回答
闹够了就滚
2楼-- · 2020-06-16 06:27

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>
    
查看更多
The star\"
3楼-- · 2020-06-16 06:35

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)
查看更多
登录 后发表回答