【发布时间】:2017-02-26 04:23:46
【问题描述】:
我正在尝试使用 React Native 打开 .pdf、.docx、.xlsx 和其他类似文件。
我目前正在将文件从 amazon s3 存储桶下载到设备,然后尝试使用 react-native-fetch-blob android.actionViewIntent(res.path(), mimeType) 在设备上打开它们。
这适用于图像,但其他任何我得到以下错误:
Error: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///storage/emulated/0/Android/data/myappname/files/1475.pdf typ=application/pdf flg=0x10000000 }
编辑:对于希望看到最终工作的实现的任何人(使用 react-native-file-system 和 react-native-fetch-blob):
const fileType = fileDownloadPath.split('.').pop();
const SavePath = Platform.OS === 'ios' ? RNFS.DocumentDirectoryPath : RNFS.ExternalDirectoryPath;
const mimeType = getMimeType(fileType); // getMimeType is a method (switch statement) that returns the correct mimetype
const path = `${SavePath}/${filename}.${fileType}`;
const android = RNFetchBlob.android;
RNFS.downloadFile({
fromUrl: url,
toFile: path,
}).promise.then(() => {
android.actionViewIntent(path, mimeType)
.then((success) => {
console.log('success: ', success)
})
.catch((err) => {
console.log('err:', err)
})
});
【问题讨论】:
-
您确定
android.actionViewIntent(res.path(), mimeType)可以处理图像文件吗?我尝试使用 source uri = res.path() 显示 png,它可以工作。但是使用 android.actionViewIntent(res.path(), 'image/png'),它会在图库应用中显示“无法生成缩略图”。
标签: android react-native