Display an image stored on phone

后端 未结 3 1395
故里飘歌
故里飘歌 2021-01-02 12:46

I have succesfully been able to take a photo inside my app and store it on the phone, but how do I display it?

I tried this static approach but it does not work (Yes

相关标签:
3条回答
  • 2021-01-02 13:13

    We should get 'READ_EXTERNAL_STORAGE' Permission


    • Import

    // import 
    import { PermissionsAndroid } from "react-native";
    
    • Function to request permission

    // function to request permission
    requestStoragePermission = async () => {
        try {
          const granted = await PermissionsAndroid.request(
            PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
            {
              title: "Permission title",
              message:
                "Permission message",
              buttonNeutral: "Ask Me Later",
              buttonNegative: "Cancel",
              buttonPositive: "OK",
            }
          );
          if (granted === PermissionsAndroid.RESULTS.GRANTED) {
            console.log("You can use the EXTERNAL_STORAGE");
          } else {
            console.log("EXTERNAL_STORAGE permission denied");
          }
        } catch (err) {
          console.warn(err);
        }
      };
    
    • Call the above Function

    <Button title="request permissions" onPress={this.requestStoragePermission} />
    

    Click this for reactnative android permissions docs

    0 讨论(0)
  • 2021-01-02 13:27

    Even I faced this issues and got it resolved. You have to prepend the the file path with file:// (mind the double /).

    <Image source={{uri:'file:///storage/emulated/0/DCIM/IMG_20161201_125218.jpg'}}
        style={myImageStyle}/>
    

    Here's a reference!

    I hope this helps :)

    0 讨论(0)
  • 2021-01-02 13:34
        <Image source = {{uri: "file:///storage/emulated/0/appiwell/image/qkpOyUaRla.jpg", 
                        width: responsiveWidth(100), 
                        height: responsiveHeight(100)}}/>
    

    Notice, should set width, height inside {uri, width, height}. Width, height in style maybe didn't work.

    responsiveWidth, responsiveHeight here https://www.npmjs.com/package/react-native-responsive-dimensions

    0 讨论(0)
提交回复
热议问题