react-native-fsを使用してファイル(pdf、word、excel、pngなど)をダウンロードしています他のアプリケーションで開くことができます。 Linkingでダウンロードしたファイルを開くことは可能ですか?Sharingを使用する場合のように、可能なアプリを使ってダイアログを開いてください。以下のコードのLinking
はファイルを開こうとしますが、通知なしでただちに終了しますが、私のアプリケーションは正常に動作しています。特定のファイルタイプのディープリンク用にURLを構築する特別な方法はありますか?最高のソリューションのための任意のアイデア?反応ネイティブの既定のアプリケーションでデバイス上のファイル(pdf、word、excel、pngなど)を開く
古いパッケージreact-native-file-openerがありますが、それはもはや維持されていません。この解決策は素晴らしいでしょう。ダウンロードコンポーネント用
簡体コード:
import React, { Component } from 'react';
import { Text, View, Linking, TouchableOpacity } from 'react-native';
import { Icon } from 'react-native-elements';
import RNFS from 'react-native-fs';
import { showToast } from '../../services/toasts';
class DownloadFile extends Component {
state = {
isDone: false,
};
handleDeepLinkPress = (url) => {
Linking.openURL(url).catch(() => {
showToast('defaultError');
});
};
handleDownloadFile =() => {
RNFS.downloadFile({
fromUrl: 'https://www.toyota.com/content/ebrochure/2018/avalon_ebrochure.pdf',
toFile: `${RNFS.DocumentDirectoryPath}/car.pdf`,
}).promise.then(() => {
this.setState({ isDone: true });
});
};
render() {
const preview = this.state.isDone
? (<View>
<Icon
raised
name="file-image-o"
type="font-awesome"
color="#f50"
onPress={() => this.handleDeepLinkPress(`file://${RNFS.DocumentDirectoryPath}/car.pdf`)}
/>
<Text>{`file://${RNFS.DocumentDirectoryPath}/car.pdf`}</Text>
</View>)
: null;
return (
<View>
<TouchableOpacity onPress={this.handleDownloadFile}>
<Text>Download File</Text>
</TouchableOpacity>
{preview}
</View>
);
}
}
export default DownloadFile;