I’m working in an app that is offline first. But here I have a problem:
Every time the users enter the APP it verifies if has image to download. If so, the app make a request to the database to download the image. The image is saved in the user’s device, so I use the addAssetsToAlbumAsync
to move it to an album.
But every time I’m calling the addAssetsToAlbumAsync
method it request the modify photo permission. I’ve already made the check if user is permission granted. Can anyone help me?
The code:
import * as FileSystem from 'expo-file-system'
import * as MediaLibrary from 'expo-media-library'
export async function downloadImage(imageUrl: string) {
console.log('Downloading image...')
return new Promise<string>((resolve, reject) => {
//const date = new Date().toISOString()
const fileUri = FileSystem.documentDirectory + `${imageUrl.split("Z-")[1]}`
console.log("fileUri =>", fileUri)
FileSystem.downloadAsync(imageUrl, fileUri)
.then((res) => res.uri)
.then((uri) => saveFile(uri))
.then(() => resolve(fileUri))
.catch((err) => {
console.log(`erro when it downloads from ${imageUrl} to ${fileUri}`)
reject(err)
})
})
}
export async function saveFile(fileUri: string) {
try {
const permission = await MediaLibrary.getPermissionsAsync()
console.log("Permission =>", permission);
if (permission.granted) {
console.log("has permission");
const albumName = 'AlbumName'
const asset = await MediaLibrary.createAssetAsync(fileUri)
const album = await MediaLibrary.getAlbumAsync(albumName)
if (album == null) {
const newAlbum = await MediaLibrary.createAlbumAsync(
albumName,
asset,
true,
)
await MediaLibrary.addAssetsToAlbumAsync(asset, newAlbum, false)
} else {
await MediaLibrary.addAssetsToAlbumAsync(asset, album, false)
}
return asset.uri
}
} catch (err) {
console.log('Save err: ', err)
}
}
The print of the request: