<io.getstream.chat.android.ui.feature.messages.composer.MessageComposerView
android:id="@+id/messageComposerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:streamUiMessageComposerAttachmentsPickerSystemPickerEnabled="true"
/>System Attachments Picker
Sometimes its not desirable for an app to have the READ_MEDIA_IMAGES and READ_MEDIA_VIDEO permissions.
In this guide, we will explain how to set up the new useDefaultSystemMediaPicker parameter in ChatUI, why it is important, and how to remove permissions from the AndroidManifest.xml using tools:node="remove".
The importance of System Media Picker
The useDefaultSystemMediaPicker parameter allows you to use the system’s default media picker instead of the custom media picker provided by the library. This can be beneficial for several reasons:
- Consistency: Provides a consistent user experience by using the familiar system media picker.
- Permissions: Reduces the need for additional permissions, as the system media picker handles permissions internally.
- Simplicity: Simplifies the implementation by leveraging the built-in functionality of the system media picker.
Setting Up System Media Picker
There are two options for enabling usage of the system media picker in XML-based UI: XML attributes and StyleTransformer.
Let’s start with the XML Attributes option.
You can enable the system media picker by setting the streamUiMessageComposerAttachmentsPickerSystemPickerEnabled attribute to true in the MessageComposerView XML layout.
You can also enable the system media picker using the StyleTransformer as shown below.
// Set the property directly in the AttachmentsPickerDialogStyle
TransformStyle.attachmentsPickerStyleTransformer = StyleTransformer { defaultStyle ->
defaultStyle.copy(
useDefaultSystemMediaPicker = true,
)
}
// Or set the property in the MessageComposerStyle
TransformStyle.messageComposerStyleTransformer = StyleTransformer { defaultStyle ->
defaultStyle.copy(
attachmentsPickerDialogStyle = defaultStyle.attachmentsPickerDialogStyle.copy(
useDefaultSystemMediaPicker = true,
),
)
}Please be advised that setting useDefaultSystemMediaPicker in MessageComposerStyle will override the value set in AttachmentsPickerDialogStyle.
Customization
By default, the system attachments picker allows selection of the following attachments:
- Picking an image/video
- Picking a file
- Capturing a photo/video
- Creating a poll
To enable/disable the default options, you can override the following properties of the
MessageComposerView:
streamUiMessageComposerAttachmentsPickerMediaAttachmentsTabEnabledto enable/disable picking an image/video.streamUiMessageComposerAttachmentsPickerFileAttachmentsTabEnabledto enable/disable picking a file.streamUiMessageComposerAttachmentsPickerCameraAttachmentsTabEnabledto enable/disable capturing a photo/video.streamUiMessageComposerAttachmentsPickerPollAttachmentsTabEnabledto enable/disable creating a poll.
Additionally, you can customize the behaviour of the visual media attachments picker by using the following properties:
streamUiMessageComposerAttachmentsPickerSystemPickerVisualMediaAllowMultipleto allow selection of multiple images/videos (default isfalse).streamUiMessageComposerAttachmentsPickerSystemPickerVisualMediaTypeto define the allowed types of visual media that can be selected. Can be one of the following:image,video,image_and_video(default isimage_and_video).
<io.getstream.chat.android.ui.feature.messages.composer.MessageComposerView
android:id="@+id/messageComposerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:streamUiMessageComposerAttachmentsPickerSystemPickerEnabled="true"
app:streamUiMessageComposerAttachmentsPickerSystemPickerVisualMediaAllowMultiple="true"
app:streamUiMessageComposerAttachmentsPickerSystemPickerVisualMediaType="image"
app:streamUiMessageComposerAttachmentsPickerCameraAttachmentsTabEnabled="false"
/>Similar to the useDefaultSystemMediaPicker property, you can set these properties in the
MessageComposerStyle or AttachmentsPickerDialogStyle using the StyleTransformer.
// Set the properties directly in the AttachmentsPickerDialogStyle
TransformStyle.attachmentsPickerStyleTransformer = StyleTransformer { defaultStyle ->
defaultStyle.copy(
useDefaultSystemMediaPicker = true,
systemMediaPickerVisualMediaAllowMultiple = true,
systemMediaPickerVisualMediaType = VisualMediaType.IMAGE,
cameraAttachmentsTabEnabled = false,
)
}
// Or set the properties in the MessageComposerStyle
TransformStyle.messageComposerStyleTransformer = StyleTransformer { defaultStyle ->
defaultStyle.copy(
attachmentsPickerDialogStyle = defaultStyle.attachmentsPickerDialogStyle.copy(
useDefaultSystemMediaPicker = true,
systemMediaPickerVisualMediaAllowMultiple = true,
systemMediaPickerVisualMediaType = VisualMediaType.IMAGE,
cameraAttachmentsTabEnabled = false,
),
)
}Removing Permissions from your Project
Let’s remove the permissions from the AndroidManifest.xml.
When using the system media picker, you can remove unnecessary permissions from your AndroidManifest.xml to streamline your app’s permission requests.
Use the tools:node="remove" attribute to remove permissions.
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" tools:node="remove" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" tools:node="remove" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" tools:node="remove" />By following these steps you can remove unnecessary permissions from your AndroidManifest.xml.