We didn't add any dependencies for working with files and images, since there are a couple of them and you might want to use different ones.
In this example, we will use react-native-image-picker, follow the instructions there to install it. After it is done we can use the image picker to select an image and send it as a message (full example with images and files can be found here):
Similar to the text message, you will need to create an image message using data from the image picker. In this example, we use
base64 just for demo purposes, but for the backend service, you will upload the image first and then send the received URL using the
To keep the UI clean, the image message renders in two different ways, if the aspect ratio is too low or too high it renders like a file message, so you don't see a narrow line on the UI. The second way is a classic image in the chat. Go give it a try.
You can use this URL https://bit.ly/2P0cn2g to test the file message presentation, remove height and width from the
imageMessage so the library will calculate it automatically and replace
uri's data with this URL.
On tap, images will be previewed inside an interactive image gallery.
In this example, we will use react-native-document-picker, follow the instructions there to install it. After it is done we can use the file picker to select a file and send it as a message (full example with images and files can be found here):
Similar to the text message, you will need to create a file message using data from the document picker. In this example,
uri will point to the local filesystem just for demo purposes, but for the backend service, you will upload the file first and then send the received URL using the
Right now, nothing will happen when a user taps on a file message, we will need to add another dependency. In this case, let's add react-native-file-viewer. As usual, follow the instructions there to install it. Now we can open a file:
Link preview works automatically, we created a separate library for that, you can found it here. Usually, however, you'll want to save the preview data so it stays the same, you can do that using
Now to choose between images and files from a single button we will use another dependency - react-native-action-sheet, please follow the instructions there to install it. If you skipped previous sections and want to use this example, remember to install - react-native-image-picker, react-native-document-picker and react-native-file-viewer. This is a drop-in example, everything should work if you had installed all the dependencies.
Don't forget to wrap your app with
@expo/react-native-action-sheet. Remember that it should be done outside a component provided in this example since we are using the
useActionSheet hook which can't be used before the component is wrapped in
ActionSheetProvider. You can create another component called
AppContainer.tsx and wrap
<App /> in the