【问题标题】:uri to real path in react-nativeuri到react-native中的真实路径
【发布时间】:2020-01-29 23:44:51
【问题描述】:

我使用 react-native-document-picker 在 Android 和 IOS 中选择图像和视频。 问题是它返回一个uri,我需要真正的路径。

我尝试使用带有函数 stat 的 react-native-fetch-blob。以及带有函数 stat 的 react-native-fs。两者都不适用于uris。但是,如果我使用例如 react-native-fs readFile,它可以与 uris 一起使用。

react-native-get-real-path 仅适用于 Android,因此不是一个选项。

编辑: 有人建议我使用 rn-fetch-blob,但我在安装它时遇到了问题: https://github.com/joltup/rn-fetch-blob/issues/289

我尝试使用react-native-get-real-path来区分Android和IOS,但也有问题。

【问题讨论】:

    标签: react-native path uri


    【解决方案1】:

    最后我使用了 ios 的 react-native-document-picker 和 android 的 react-native-file-picker

    【讨论】:

      【解决方案2】:
      File file = new File(getURIPath(uriValue));
      
      /**
       * URI Value
       * @return File Path.
       */
      String getURIPath(Uri uriValue) 
          {
              String[] mediaStoreProjection = { MediaStore.Images.Media.DATA };
              Cursor cursor = getContentResolver().query(uriValue, mediaStoreProjection, null, null, null);
              if (cursor != null){ 
              int colIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
              cursor.moveToFirst();
              String colIndexString=cursor.getString(colIndex);
              cursor.close();
              return colIndexString;
              }
              return null;
          }
      

      【讨论】:

      • 请正确格式化此代码以使其易于阅读。 :)
      猜你喜欢
      • 1970-01-01
      • 2013-07-06
      • 1970-01-01
      • 1970-01-01
      • 2017-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多