Hi, please help me
- SDK Version: “expo”: “^42.0.3”
- Platforms(Android/iOS/web/all): android 10 or 11
- expo-media-library
Repro here: https://snack.expo.dev/@aemam/image-picker
can anyone kindly please tell me why MediaLibrary.saveToLibraryAsync(local); is working
while MediaLibrary.createAssetAsync(fileUri); is NOT ?
Thanks in advance !
by the way, i found that:
https://github.com/expo/expo/issues/11165
but iam using expo
====================================================
import * as FileSystem from 'expo-file-system';
import React, { useState, useEffect } from 'react';
import { Button, Image, View, Platform } from 'react-native';
import * as ImagePicker from 'expo-image-picker';
import * as MediaLibrary from 'expo-media-library';
export default function ImagePickerExample() {
const [image, setImage] = useState(null);
useEffect(() => {
(async () => {
await MediaLibrary.requestPermissionsAsync();
})();
}, []);
const pickImage = async () => {
let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.All,
allowsEditing: true,
aspect: [4, 3],
quality: 1,
});
console.log(result);
try {
const { status } = await MediaLibrary.requestPermissionsAsync();
if (status === "granted") {
let fileUri = FileSystem.cacheDirectory + "program.pdf";
let saveResult = await MediaLibrary.saveToLibraryAsync(result.uri);//'file:///data/user/0/host.exp.exponent/files/ExperienceData/%2540aemam%252FRoyalProtocolPo/program.pdf');
//alert('save Result ' + saveResult + ' df df' + result.uri);
console.log(saveResult);
const file = await FileSystem.downloadAsync('http://www.africau.edu/images/default/sample.pdf', fileUri)
.then((rrr) => {
//alert(JSON.stringify(rrr)); //200 success
saveFile(rrr.uri, ' save Result ' + saveResult + ' df df' + result.uri, result.uri);
})
.catch(error => { //the idea of then & catch after the await is nice !
alert('تعذر تحميل الملف');
console.error(error);
})
} else {
alert('يجب السماح للتطبيق الوصول لملفات الهاتف')
}
} catch (e) {
alert(e.message);
}
if (!result.cancelled) {
setImage(result.uri);
}
};
const saveFile = async (fileUri, str, local) => {
try {
//const ios = Platform.OS === 'ios'
//Why this is working ? const asset = await MediaLibrary.saveToLibraryAsync(local);
//and the following Not ? Unable to copy file into "external" storage.
const asset = await MediaLibrary.createAssetAsync(fileUri); //https://github.com/expo/expo/issues/11165
alert(str + ' 3adda ka ' + asset);
const assetLink = await MediaLibrary.createAlbumAsync("RPDownloads", asset, true); //false to cut not copy ;)
console.log(asset);
alert(` تم تحميل الملف بنجاح فى ملف بهاتفك RPDownloads: ${asset.filename}`);
alert('working ' + local + ' Not working ' + fileUri);
//const imageFileExts = ['jpg', 'png', 'gif', 'heic', 'webp', 'bmp'];
} catch (e) {
alert(str +' -- ' + e.message);
}
}
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button title="Pick an image from camera roll" onPress={pickImage} />
{image && (
<Image source={{ uri: image }} style={{ width: 200, height: 200 }} />
)}
</View>
);
}
====================================
{
"dependencies": {
"expo-image-picker": "~10.2.3",
"expo-media-library": "~12.1.2",
"expo-file-system": "~11.1.3"
}
}