如何从expo图像选择器react native中选择图像



我尝试从图库中选择图像,并将其显示在图像组件上。我阅读了世博会的文件并按照步骤进行操作。当我在网上试用时,它是有效的。但当我在安卓手机上尝试时,在我选择图像并裁剪我的应用程序后重新下载。我不知道哪里出错了

  • 我的代码
import React, { useState, useEffect } from 'react';

import * as ImagePicker from 'expo-image-picker';
import { Button, Image, View, Platform } from 'react-native';



export default function GalleryComponenet() {
const [image, setImage] = useState(null);

useEffect(() => {
(async () => {
if (Platform.OS !== 'web') {
const { status } = await ImagePicker.requestMediaLibraryPermissionsAsync();
if (status !== 'granted') {
alert('Sorry, Camera roll permissions are required to make this work!');
}
}
})();
}, []);

const chooseImg = async () => {
let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.All,
aspect: [4, 3],
quality: 1,         
allowsEditing: true,
});

console.log(result);

if (!result.cancelled) {
setImage(result.uri);
}
};

return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center', }}>     
<Button title="Choose image from camera roll" onPress={chooseImg} />
{image && <Image source={{ uri: image }} style={{ width: 200, height: 200 }} />}
</View>
);
}

我没有发现你的错误,但刚刚完成了从图库中拾取图像并返回她的uri的功能。

import * as ImagePicker from "expo-image-picker";
import { Platform, Alert } from "react-native";
export default async function getPhotoUri() {
if (Platform.OS !== "web") {
const { status } = await ImagePicker.requestMediaLibraryPermissionsAsync();
if (status !== "granted") {
Alert.alert("OOPS!", "Premmision denied", [{ text: "ok" }]);
} else {
const result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.All,
allowsEditing: true,
aspect: [4, 3],
quality: 1,
});
if (!result.cancelled) {
return result.uri;
}
}
}
}

最新更新