react-native-fs - Error: Attempt to invoke virtual method 'byte[] java.lang.String.getBytes()' on a null object reference

Viewed 294

I was try to pick a photo with react-native-image-picker, this works. So I tried to get the photo path that I choose with image picker and store a copy in diferent path with react-native-fs.

But when I try this second step I receive a error.

[Error: Attempt to invoke virtual method 'byte[] java.lang.String.getBytes()' on a null object reference]

I dont know if its a wrong configuration on /android for react-native-fs works, or any else

import React, { useState } from 'react';
import {
  Text,
  View,
  Image,
  Button,
  Alert
} from 'react-native';

import RNFS from 'react-native-fs'

const path = RNFS.ExternalDirectoryPath + '/test.jpeg';

import {launchImageLibrary} from 'react-native-image-picker';

const App = () => {

  const [uri, setUri] = useState('d')

  return (
    <>
      <Text>jhhhh{uri}</Text>
      <Image
        style={{width: 100, height: 100}}
        source={{uri:uri}}
      />
      <Button
        onPress={()=>{
          launchImageLibrary( { mediaType: 'photo', includeBase64: true }, result => {
            console.log(result.assets[0].uri)
            setUri(result.assets[0].uri)

            console.log( RNFS.ExternalDirectoryPath )

            RNFS.writeFile(path,result.base64, 'base64')
            .then(
              () => Alert(path)
            ).catch(e => console.log(e))

          } )
        }}
        title="GO"
      />
    </>
  )
};

export default App;

note: I noticed that the recent version of react already does auto linkg and the react-native-fs doc says to do this manually, so I skipped the last step. When I did the last step it returned with an error saying the import was about writing itself.

Edit: DocumentDirectoryPath or ExternalDirectoryPath I received the same error

0 Answers
Related