如何在react-native中获取文件的绝对路径? [英] How to get Absolute path of a file in react-native?
问题描述
我正在寻找 react-native 中的文件选择器,它会返回所选择文件的绝对路径.我目前正在使用 react-native-document-picker,但它给了我content://com.android.providers.media.documents/document/......
格式的相对路径.因为我想压缩我的视频文件,像 react-native-ffmpeg 和react-native-video-processing 需要文件的绝对路径.
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 relative path in the format of content://com.android.providers.media.documents/document/......
. As I want to compress my video file, libraries like react-native-ffmpeg and react-native-video-processing require Absolute path of a file.
推荐答案
其实我自己已经想通了.您可以通过 3 种方式获取绝对路径.
I actually figured this out myself. You can get Absolute path in 3 ways.
最方便的方式:使用react-native-document-picker,在选择时它会给你一个相对路径,就像这样content://com.android......
.将该相对路径传递给 Stat(filepath)react-native-fetch-blob
库的 函数.该对象将返回绝对路径.使用 file://
附加路径以用于进一步操作.
The most convenient way : Use react-native-document-picker, on selection it will give you a Relative path, something like this content://com.android......
. Pass that Relative path to Stat(filepath) function of the react-native-fetch-blob
library. The object will return absolute path. Append the path with file://
to use it for further operations.
另外两种方式是使用 react-native-image picker 和 CameraRoll (React Native Library)
The other 2 ways are by using react-native-image picker and CameraRoll (React Native Library)
我希望这会有所帮助!
请确保您在硬件设备而不是虚拟设备上运行应用程序以进行测试.
Please make sure you run the app on hardware device rather than Virtual Device to test it.
这篇关于如何在react-native中获取文件的绝对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!