当我们点击上传图片按钮时,出现了以下错误。

huangapple go评论85阅读模式
英文:

When we pressed upload image button, the following error shown

问题

错误:警告:图像选择器结果中的键“cancelled”已过时,将在SDK 48中移除,请改用“canceled”代替。
警告:图像选择器结果中的键“uri”已过时,将在SDK 48中移除,您可以通过“assets”数组访问所选资源。

英文:

Please check error, the error show: Key "cancelled" in the image picker result is deprecated. By the way I used canceled

import React, { useEffect, useState } from 'react'
import { Alert, Button, Image, View, StyleSheet,Platform } from 'react-native'
import * as ImagePicker from 'expo-image-picker'
import Constants from 'expo-constants'


const Fine_Repair_Request = () => {
 const [image,setimage] = useState(null);

 useEffect( async() => {
  if(Platform.OS !== 'web'){
    const {status} =await ImagePicker.requestMediaLibraryPermissionsAsync();
    if(status !== 'granted'){
      alert('Permission denied')
    }
  }
 },[])
   
   const PickImage = async()=>{
    let result = await ImagePicker.launchImageLibraryAsync({
      mediaTypes: ImagePicker.MediaTypeOptions.All,
      allowsEditing:true,
      aspect:[4,3],
      quality:1
    })
    console.log(result)
    if(!result.canceled){
      setimage(result.uri)
    }
   }


  return (
    <View style={styles.container}>
      <Button title="Upload Image" onPress={PickImage} />
      {image && <Image source={{uri:image}}/>}
    </View>
  )
}
export default Fine_Repair_Request;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center'
  }
})
 Error:  WARN  Key "cancelled" in the image picker result is deprecated and will be removed in SDK 48, use "canceled" instead
 WARN  Key "uri" in the image picker result is deprecated and will be removed in SDK 48, you can access selected assets through the "assets" array instead

答案1

得分: 1

所以,您的问题是您正在访问一个已弃用的属性,并且没有正确访问图像的 URI。

expo-image-picker 返回的结果响应如下所示:

要能够获取所选资源的 URI,您需要访问 assets 数组,然后访问图像:

console.log(result.assets[0].uri) // 将打印第一个选择的图像的 URI

cancelled 属性也已被弃用,所以只需使用他们建议的 canceled 键。

英文:

So, your problem is that you are accessing a deprecated propertie and you aren't accessing ok to the uri of the image.

From the expo-image-picker this is what a result response looks like:

// When you run this example and pick an image, you will see the image that you picked show up in your app, and a similar log will be shown in the console:

{
  "assets": [
    {
      "assetId": "C166F9F5-B5FE-4501-9531",
      "base64": null,
      "duration": null,
      "exif": null,
      "fileName": "IMG.HEIC",
      "fileSize": 6018901,
      "height": 3025,
      "type": "image",
      "uri": "file:///data/user/0/host.exp.exponent/cache/cropped1814158652.jpg"
      "width": 3024
    }
  ],
  "canceled": false,
  "cancelled": false
}

In order to be able to get the uri of the selected asset you have to access the assets array and then the image:

console.log(result.assets[0].uri) // Will print the uri of the first selected image

The propertie cancelled is deprecated also, so just use the key that they are suggesting to you canceled

答案2

得分: 1

移除了 console.log 后,错误消息消失了。

英文:

removing the console.log got rid of the error message for me .

答案3

得分: 0

使用以下代码部分:

result?.assets[0]?.uri

而不是

result?.uri
英文:

use

result?.assets[0]?.uri

instead of

result?.uri

答案4

得分: -1

for Error: WARN Key "cancelled" try removing the console.log and add delete result.cancelled;

const result = await ImagePicker.launchImageLibraryAsync();
if (!result.canceled) {
///// add this line
delete result.cancelled;

.......
}

and for WARN Key "uri"

setImage(result.assets[0].uri)

//instead of

setImage(result.uri)

then the full code :

const PickImage = async() => {
let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.All,
allowsEditing: true,
aspect: [4, 3],
quality: 1
})
if (!result.canceled) {
delete result.cancelled;
setimage(result.assets[0].uri)
}
}

英文:

for Error: WARN Key "cancelled" try removing the console.log and add delete result.cancelled;

const result = await ImagePicker.launchImageLibraryAsync();
    // Explore the result
    //console.log(result);
    if (!result.canceled) {
      ///// add this line
      delete result.cancelled;

      .......

    }
  }

and for WARN Key "uri"

setImage(result.assets[0].uri)

//instead of

setImage(result.uri)

then the full code :

   const PickImage = async()=>{
    let result = await ImagePicker.launchImageLibraryAsync({
      mediaTypes: ImagePicker.MediaTypeOptions.All,
      allowsEditing:true,
      aspect:[4,3],
      quality:1
    })
    // console.log(result) to delete
    if(!result.canceled){
      delete result.cancelled;
      setimage(result.assets[0].uri)
    }
   }

huangapple
  • 本文由 发表于 2023年7月17日 15:00:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702127.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定