javascript - Problem to save picture to mediaLibrary with expo - Stack Overflow

i'm pletly new to react and react native too, but i'm trying to learn it.I'm having a pr

i'm pletly new to react and react native too, but i'm trying to learn it.

I'm having a problem when i try to save picture to my phone gallery

So, i'm importing

import { MediaLibrary } from 'expo-media-library'

also i'm usigng states

const [hasPermission, setHasPermission] = useState(null);
const [type, setType] = useState(Camera.Constants.Type.back);
const [cameraRef, setCameraRef] = useState(null)

and here all works fine except saving the photo

<TouchableOpacity style={{alignSelf: 'center'}} onPress={async() => {
            if(cameraRef){
              let photo = await cameraRef.takePictureAsync();
              console.log('photo', photo);
              MediaLibrary.saveToLibraryAsync(photo.uri)

            }
          }}>

in my console log i see the object

    photo Object {
     "height": 4156,
     "uri": "file:///var/mobile/Containers/Data/Application/B7CCEDB6-DFC5-4898-BD70-B2FF1159FC1B/Library/Caches/ExponentExperienceData/%2540anonymous%252Ftest-5bfa90d8-12e9-44fe-a19d-69bb5eeb74b9/Camera/D783C734-29B9-489B-9798-A0737388E93C.jpg",
     "width": 2376,
}

But i'm not able to find a way to save it to camera roll, i'm always getting this error

[Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_expoMediaLibrary.MediaLibrary.saveToLibraryAsync')]

Any help would be appreciate

R.

i'm pletly new to react and react native too, but i'm trying to learn it.

I'm having a problem when i try to save picture to my phone gallery

So, i'm importing

import { MediaLibrary } from 'expo-media-library'

also i'm usigng states

const [hasPermission, setHasPermission] = useState(null);
const [type, setType] = useState(Camera.Constants.Type.back);
const [cameraRef, setCameraRef] = useState(null)

and here all works fine except saving the photo

<TouchableOpacity style={{alignSelf: 'center'}} onPress={async() => {
            if(cameraRef){
              let photo = await cameraRef.takePictureAsync();
              console.log('photo', photo);
              MediaLibrary.saveToLibraryAsync(photo.uri)

            }
          }}>

in my console log i see the object

    photo Object {
     "height": 4156,
     "uri": "file:///var/mobile/Containers/Data/Application/B7CCEDB6-DFC5-4898-BD70-B2FF1159FC1B/Library/Caches/ExponentExperienceData/%2540anonymous%252Ftest-5bfa90d8-12e9-44fe-a19d-69bb5eeb74b9/Camera/D783C734-29B9-489B-9798-A0737388E93C.jpg",
     "width": 2376,
}

But i'm not able to find a way to save it to camera roll, i'm always getting this error

[Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_expoMediaLibrary.MediaLibrary.saveToLibraryAsync')]

Any help would be appreciate

R.

Share Improve this question asked Apr 28, 2020 at 16:36 user2653710user2653710 411 silver badge5 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

I think you're getting undefined object error with promise since you did not also concerned MediaLibrary.saveToLibraryAsync() with await.

You should maybe put MediaLibrary.saveToLibraryAsync() with await too just like you did on takePictureAsync() since it is a async function.

let photo = await cameraRef.takePictureAsync();
console.log('photo', photo);
await MediaLibrary.saveToLibraryAsync(photo.uri);

Just an assumption, hope it will help you to solve it.

This was happening due to import issue.

// Change from this
import { MediaLibrary } from 'expo-media-library';
// To this
import * as MediaLibrary from 'expo-media-library'; 

This was answered in below thread. Mentioning here because if someone may e across this issue again.

[Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_expoMediaLibrary.MediaLibrary.createAssetAsync')]

For me the problem was I followed the docs

https://docs.expo.dev/versions/latest/sdk/media-library/

and requested permissions:

if (permissionResponse.status !== 'granted') {
      await requestPermission();
    }

According to this github issue from expo team https://github./expo/expo/issues/9027

You don't need to ask for permission here.

So it seems the documentation should get updated

When I deleted asking for permissions, MediaLibrary.saveToLibraryAsync() asks for permission by itself and allows saving videos to my camera roll.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745314214a4622139.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信