Skip to content

Commit

Permalink
Save image to user media library when taken from camera during compos…
Browse files Browse the repository at this point in the history
…ing (#3180)

* save images to media library when taken from camera

* ensure we have access to media library

* `canAskAgain`

* just use MediaLibrary directly to avoid duplication
  • Loading branch information
haileyok authored Mar 12, 2024
1 parent ee57d74 commit 80cc1f1
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/view/com/composer/photos/OpenCameraBtn.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, {useCallback} from 'react'
import {TouchableOpacity, StyleSheet} from 'react-native'
import * as MediaLibrary from 'expo-media-library'
import {
FontAwesomeIcon,
FontAwesomeIconStyle,
Expand All @@ -24,26 +25,42 @@ export function OpenCameraBtn({gallery}: Props) {
const {track} = useAnalytics()
const {_} = useLingui()
const {requestCameraAccessIfNeeded} = useCameraPermission()
const [mediaPermissionRes, requestMediaPermission] =
MediaLibrary.usePermissions()

const onPressTakePicture = useCallback(async () => {
track('Composer:CameraOpened')
try {
if (!(await requestCameraAccessIfNeeded())) {
return
}
if (!mediaPermissionRes?.granted && mediaPermissionRes?.canAskAgain) {
await requestMediaPermission()
}

const img = await openCamera({
width: POST_IMG_MAX.width,
height: POST_IMG_MAX.height,
freeStyleCropEnabled: true,
})

// If we don't have permissions it's fine, we just wont save it. The post itself will still have access to
// the image even without these permissions
if (mediaPermissionRes) {
await MediaLibrary.createAssetAsync(img.path)
}
gallery.add(img)
} catch (err: any) {
// ignore
logger.warn('Error using camera', {error: err})
}
}, [gallery, track, requestCameraAccessIfNeeded])
}, [
gallery,
track,
requestCameraAccessIfNeeded,
mediaPermissionRes,
requestMediaPermission,
])

const shouldShowCameraButton = isNative || isMobileWeb
if (!shouldShowCameraButton) {
Expand Down

0 comments on commit 80cc1f1

Please sign in to comment.