本地应用打开文件 react-native-file-opener
NPM install
1npm install --save react-native-file-openerIOS install
12在Xcode中,将node_modules/react-native-file-opener/ios/RNFileOpener.xcodeproj添加到项目的Libraries目录;将Libraries/RNFileOpener.xcodeproj/Products/libRNFileOpener.a拖拽到Build Phases -> Link Binary With Libraries中。Android install
1234// file: android/settings.gradle...include ':react-native-file-opener'project(':react-native-file-opener').projectDir = new File(settingsDir, '../node_modules/react-native-file-opener/android')123456// file: android/app/build.gradle...dependencies {...compile project(':react-native-file-opener')}1234567891011121314// file: MainActivity.java...import com.fileopener.FileOpenerPackage;//<- import packagepublic class MainActivity extends ReactActivity {@Overrideprotected List<ReactPackage> getPackages() {return Arrays.<ReactPackage>asList(new MainReactPackage(),new FileOpenerPackage() //<- Add package);}...}Usage
在上一篇React-Native-上传下载例子的基础上,添加如下代码1234567891011121314151617181920...import FileOpener from 'react-native-file-opener';...class TestFileStream extends Component {...openFile(fileName) {const FilePath = Platform.OS === 'android' ? dirs.DCIMDir + '/' + fileName : dirs.DocumentDir + '/' + fileName;const FileMimeType = 'image/jpg';FileOpener.open(FilePath,FileMimeType).then((msg) => {console.log('success!!')},(err) => {console.log('打开失败:', err)});}...}export default TestFileStream;