Lai32290
Lai32290

Reputation: 8578

How to save image to Camera Roll using Expo?

I'm creating an app using Expo to take picture, I'm already possible to save pictures to FileSystem.documentDirectory, but this is not what I want.

I tried to save image to Camera Roll using code like this:

import { CameraRoll } from 'react-native';
...
...
await CameraRoll.saveToCameraRoll(photo.uri);

But it's returning a warning me to use react-native-cameraroll instead of react-native. But as I see in the document of the react-native-cameraroll, it's seems not supporting Expo.

Is there any way to save image to Camera Roll in Expo?

Upvotes: 16

Views: 19440

Answers (2)

In Seo
In Seo

Reputation: 21

Sometimes documents assume that we know more.

Please refer the following code snapshot.

const downloadPhoto = async (photoUrl) =>{
    const fileName = photoUrl.replace(/^.*[\\\/]/, '');
    let imageFullPathInLocalStorage = FileSystem.documentDirectory+fileName;
    return new Promise(async (resolve)=>{
        FileSystem.downloadAsync(
            photoUrl,
            imageFullPathInLocalStorage
        )
        .then(async ({ uri }) => {
            MediaLibrary.saveToLibraryAsync(imageFullPathInLocalStorage);
            return resolve(imageFullPathInLocalStorage);
        })
    });
  }

You should specify the local file URI to save the downloaded file.

Upvotes: 2

Lai32290
Lai32290

Reputation: 8578

I found the solution to this, instead of using CameraRoll you can use MediaLibrary to MediaLibrary.saveToLibraryAsync(localUri)

More details on the documentation

Upvotes: 31

Related Questions