Ron*_*obo 4 react-native react-native-android
我正在寻找react-native中的文件选择器,该选择器返回我选择的文件的绝对路径。我目前正在使用react-native-document-picker,但它以的格式提供了相对路径content://com.android.providers.media.documents/document/......。当我要压缩视频文件时,像react-native-ffmpeg和react-native-video-processing这样的库都需要文件的绝对路径。
我实际上是自己想出来的。您可以通过3种方式获得绝对路径。
最方便的方法:使用react-native-document-picker,选择后会给您一个相对路径,类似这样content://com.android......。将该相对路径传递给库的Stat(filepath)函数react-native-fetch-blob。该对象将返回绝对路径。附加路径file://以将其用于进一步的操作。
另两种方式是通过使用react-native-image picker和CameraRoll(React Native Library)
我希望这有帮助 !
安装react-native-fetch-blob以获取文件的路径。下面是一个例子。
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;
}
}};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5412 次 |
| 最近记录: |