How to get Absolute path of a file in react-native?

后端 未结 4 736
独厮守ぢ
独厮守ぢ 2020-12-17 23:58

I am looking for a file picker in react-native which returns me Absolute Path of the file picked. I am currently using react-native-document-picker, but it gives me the rela

4条回答
  •  轮回少年
    2020-12-18 00:42

    Install react-native-fetch-blob to get the path of the file. Below is an example.

    pickFile = async () => {
    try {
      const res = await DocumentPicker.pick({
        type: [DocumentPicker.types.allFiles],
      });
    
      console.log(res.uri);
      //output: content://com.android.providers.media.documents/document/image%3A4055
    
      RNFetchBlob.fs
        .stat(res.uri)
        .then((stats) => {
          console.log(stats.path);
     //output: /storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20200831-WA0019.jpg
        })
        .catch((err) => {
          console.log(err);
        });
    } catch (err) {
      if (DocumentPicker.isCancel(err)) {
      } else {
        throw err;
      }
    }};
    

提交回复
热议问题