集成react-native-image-picker時,報錯Couldn't get file path for photo


1. 版本環境:

"react": "16.13.1",
"react-native": "0.63.2",
"react-native-image-picker": "^2.3.3",
targetSdkVersion: 29。

問題還出現在:Android 10/ API 29..

2.出問題時代碼:

<TouchableOpacity 
      style={styles.addBtn}
      onPress={() => this.addImages(options)}>
      <Text style={styles.addText}>添加照片</Text>
</TouchableOpacity>
addImages = (options) => {
        let { imgUrlArray } = this.state;
        let length = imgUrlArray.length;
        console.log(length)
        if(length === 9) {
            return;
        }

        ImagePicker.showImagePicker(options, (response) => {
            
            console.log('Response = ', response);
            
            if (response.didCancel) {
                console.log('User cancelled image picker');
            } else if (response.error) {
                console.log('ImagePicker Error: ', response.error);
            } else if (response.customButton) {
                console.log('User tapped custom button: ', response.customButton);
            } else {
                const source = { uri: response.uri, id: length + 1 + '' };
                
                // You can also display the image using data:
                // const source = { uri: 'data:image/jpeg;base64,' + response.data };
                imgUrlArray.push(source)
                this.setState({
                    imgUrlArray: imgUrlArray,
                });
            }
        });
    }

點擊后無反應,但是進入了函數。

3.解決方法:

方案一:

  android/app/src/main/AndroidManifest.xml    在application標簽中加入  

  android:requestLegacyExternalStorage ="true"

  這個辦法,能夠應用短期修復程序來解決此問題

  我認為這是由於Android 10范圍內的存儲隱私更改所致,並且將需要更新該庫以及它如何從文件系統讀取/寫入。但是我的Android開發經驗非常有限。

方案二:
import { PermissionsAndroid } from “react-native”
    
    
    //功能函數中加入...     const granted
= await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, { title: 'We need your permission' }, ); if (granted === PermissionsAndroid.RESULTS.GRANTED) { console.log('You can use the camera'); ImagePicker.showImagePicker(options, async (response) => { if (response.didCancel) { console.log('User cancelled image picker'); } else if (response.error) { console.log('ImagePicker Error: ', response.error); console.log(response); } else if (response.customButton) { console.log('User tapped custom button: ', response.customButton); } else { const source = { uri: response.uri }; } }); } else { console.log('Camera permission denied'); }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM