Save image to user media library when taken from camera during composing (#3180)
* save images to media library when taken from camera * ensure we have access to media library * `canAskAgain` * just use MediaLibrary directly to avoid duplicationzio/stable
parent
ee57d74765
commit
80cc1f18a2
|
@ -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,
|
||||
|
@ -24,6 +25,8 @@ 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')
|
||||
|
@ -31,6 +34,9 @@ export function OpenCameraBtn({gallery}: Props) {
|
|||
if (!(await requestCameraAccessIfNeeded())) {
|
||||
return
|
||||
}
|
||||
if (!mediaPermissionRes?.granted && mediaPermissionRes?.canAskAgain) {
|
||||
await requestMediaPermission()
|
||||
}
|
||||
|
||||
const img = await openCamera({
|
||||
width: POST_IMG_MAX.width,
|
||||
|
@ -38,12 +44,23 @@ export function OpenCameraBtn({gallery}: Props) {
|
|||
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) {
|
||||
|
|
Loading…
Reference in New Issue